• BZOJ1293: [SCOI2009]生日礼物


    1293: [SCOI2009]生日礼物

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 983  Solved: 526
    [Submit][Status]

    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。

    Source

     题解:
    考虑到如果线段的左端点定了,那么每种肯定会选离这个端点最近的而又在这个端点右边的彩珠。
    所以我们维护一个k个元素的堆,每次取出队首元素将其弹出,加入它所属种类的下一个彩珠
    如果当前取出的彩珠是该种类彩珠的最后一个,那么跳出循环,输出答案。
    我用的vector和priority_queue
    代码:
     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<iostream>
     7 #include<vector>
     8 #include<map>
     9 #include<set>
    10 #include<queue>
    11 #define inf 1<<30
    12 #define maxn 500+100
    13 #define maxm 500+100
    14 #define eps 1e-10
    15 #define ll long long
    16 #define pa pair<int,int>
    17 using namespace std;
    18 inline int read()
    19 {
    20     int x=0,f=1;char ch=getchar();
    21     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    22     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    23     return x*f;
    24 }
    25 vector<int>a[61];
    26 priority_queue<pa,vector<pa>,greater<pa> >q;
    27 int n,k,mi,mx,ans;
    28 int main()
    29 {
    30     freopen("input.txt","r",stdin);
    31     freopen("output.txt","w",stdout);
    32     n=read();k=read();
    33     for(int i=1;i<=k;i++)
    34     {
    35      int x=read(),y; 
    36      for(int j=1;j<=x;j++)y=read(),a[i].push_back(y);
    37     }  
    38     mi=inf;mx=0;
    39     for(int i=1;i<=k;i++)
    40     {
    41      q.push(make_pair(a[i][0],(i-1)*n+1));    
    42      if(a[i][0]>mx)mx=a[i][0];if(a[i][0]<mi)mi=a[i][0];
    43     }
    44     ans=mx-mi;//cout<<mx<<' '<<mi<<endl;
    45     while(1)
    46      {
    47          pa now=q.top();q.pop();
    48          int x=(now.second-1)/n+1,y=(now.second-1)%n+1;
    49          //cout<<now.first<<' '<<now.second<<' '<<x<<' '<<y<<' '<<a[x].size()<<endl;
    50          if(y==a[x].size())break;
    51          q.push(make_pair(a[x][y],(x-1)*n+y+1));
    52          mi=q.top().first;
    53          if(a[x][y]>mx)mx=a[x][y];
    54          ans=min(ans,mx-mi);//cout<<mx<<' '<<mi<<endl;
    55      }
    56     printf("%d
    ",ans); 
    57     return 0;
    58 }
    View Code

     UPD:也可以直接把每一种颜色用链表串起来,程序的常数会更小。

  • 相关阅读:
    nginx 启动相关的
    爬取豆瓣读书/文件存储数据/数据库存储数据
    python Web 开发三剑客比较
    scrapy
    爬虫自动登录抽屉
    组合搜索
    html瀑布流
    Ajax上传文件/文件预览
    Form组件
    django分页
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3930654.html
Copyright © 2020-2023  润新知