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

时间:2023-11-10 19:29:20

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=;
while(c<''||c>'') c=getchar();
while(''<=c&&c<='') x=x*+(c^),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[],tot,ans=1e9;
int main(){
read(n);read(k); register int i,j,q;
for(i=;i<=k;++i){
read(j);
while(j--) read(q),a[++m]=(data){q,i};
}sort(a+,a+m+,cmp);
i=; j=;
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 ;
}