• BZOJ1293: [SCOI2009]生日礼物


    1293: [SCOI2009]生日礼物

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 2590  Solved: 1424
    [Submit][Status][Discuss]

    Description

    小西有一条很长的彩带,彩带上挂着各式各样的彩珠。已知彩珠有N个,分为K种。简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置)。某些坐标上可以没有彩珠,但多个彩珠也可以出现在同一个位置上。 小布生日快到了,于是小西打算剪一段彩带送给小布。为了让礼物彩带足够漂亮,小西希望这一段彩带中能包含所有种类的彩珠。同时,为了方便,小西希望这段彩带尽可能短,你能帮助小西计算这个最短的长度么?彩带的长度即为彩带开始位置到结束位置的位置差。

    Input

    第一行包含两个整数N, K,分别表示彩珠的总数以及种类数。接下来K行,每行第一个数为Ti,表示第i种彩珠的数目。接下来按升序给出Ti个非负整数,为这Ti个彩珠分别出现的位置。

    Output

    应包含一行,为最短彩带长度。

    Sample Input

    6 3
    1 5
    2 1 7
    3 1 3 8

    Sample Output

    3

    HINT

    有多种方案可选,其中比较短的是1~5和5~8。后者长度为3最短。
    【数据规模】
    对于50%的数据, N≤10000;
    对于80%的数据, N≤800000;
    对于100%的数据,1≤N≤1000000,1≤K≤60,0≤彩珠位置<2^31。

    思路{

      直接两个指针模拟一下即可.细节有点多...

    }

    #include<bits/stdc++.h>
    #define il inline
    #define RG register
    #define ll long long
    #define db double
    #define N 1000010
    using namespace std;
    struct Dat{
      ll pos,col;
      void read(int x){scanf("%lld",&pos);col=x;}
      bool operator <(const Dat & a)const{return pos<a.pos;}
    }a[N];
    int n,k,tot,cnt[N],sum;
    void Modify(int &x,int &y){
      for(int i=x;i<=y;++i){
        cnt[a[i].col]--;
        if(!cnt[a[i].col])sum--;
      }
      x=y+1;y++;
    }
    int main(){
      scanf("%d%d",&n,&k);
      for(int i=1;i<=k;++i){
        int tt;scanf("%d",&tt);
        for(int j=1;j<=tt;++j)a[++tot].read(i);
      }sort(a+1,a+tot+1);
      ll Ans(1<<30);Ans*=17;
      int l(1),r(1),nxt(1);a[0].pos=a[1].pos;
      while(a[l].pos==a[l+1].pos){
        if(!cnt[a[l].col])sum++;
        cnt[a[l].col]++,l++,r++;
      }
      if(!cnt[a[l].col])sum++;
      cnt[a[l].col]++;
      if(sum>=k)cout<<"0",exit(0);
      for(;l<=tot;Modify(nxt,l)){
        if(sum==k){Ans=min(Ans,(ll)a[r].pos-a[l].pos);continue;}
        for(r++;r<=tot;r++){
          if(!cnt[a[r].col]){
        sum++;cnt[a[r].col]++;
        if(sum==k){
          Ans=min(Ans,(ll)a[r].pos-a[l].pos);
          break;
        }
          }else cnt[a[r].col]++;
        }
      }cout<<Ans;
      return 0;
    }
    
  • 相关阅读:
    模块
    time/datetime/random/string/os/sys/shutil/zipfile/tarfile
    模块
    模块
    模块
    2.1
    1.4
    生成器 迭代器
    闭包 装饰器
    函数
  • 原文地址:https://www.cnblogs.com/zzmmm/p/7512959.html
Copyright © 2020-2023  润新知