P2564 [SCOI2009]生日礼物(尺取法)

P2564 [SCOI2009]生日礼物

三个字。尺取法.........

坐标按x轴排序。

蓝后尺取一下..........

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int Min(int a,int b){return a<b?a:b;}
void read(int &x){
    static char c=getchar();x=0;
    while(c<'0'||c>'9') c=getchar();
    while('0'<=c&&c<='9') x=x*10+(c^48),c=getchar();
}
#define N 1000005
struct data{int x,y;}a[N];
inline bool cmp(data A,data B){return A.x<B.x;}
int n,m,k,col[65],tot,ans=1e9;
int main(){
    read(n);read(k); register int i,j,q;
    for(i=1;i<=k;++i){
        read(j);
        while(j--) read(q),a[++m]=(data){q,i};
    }sort(a+1,a+m+1,cmp);
    i=0; j=1;
    while(i<n){
        ++i;
        if(!col[a[i].y]) ++tot;
        col[a[i].y]=a[i].x;
        while(col[a[j].y]!=a[j].x&&j<=i) ++j;
        if(tot>=k) ans=Min(ans,a[i].x-a[j].x);
    }printf("%d",ans);
    return 0;
}

 

posted @ 2019-03-23 16:15  kafuuchino  阅读(197)  评论(0编辑  收藏  举报