12.17省选模拟t1 生日礼物
题目
链接:https://xjoi.net/contest/3538/problem/1
另见SCOI2009 生日礼物
分析
显然是个双指针,然后就没了。
代码
#include<bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x){
x=0;char ch=getchar();bool f=false;
while(!isdigit(ch)){if(ch=='-'){f=true;}ch=getchar();}
while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
x=f?-x:x;
return ;
}
template <typename T>
inline void write(T x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10^48);
return ;
}
const int N=1000005;
int n,k,op,idx,cnt[N],now,ans=0x3f3f3f3f,hh,tt=-1,que[N];
struct node{int x,id;}a[N];
inline bool cmp(node x,node y){return x.x<y.x;}
int main(){
read(n),read(k);
for(int i=1;i<=k;i++){
read(op);
for(int j=1;j<=op;j++){
read(a[++idx].x);
a[idx].id=i;
}
}
sort(a+1,a+idx+1,cmp);
for(int i=1;i<=n;i++){
if(cnt[a[i].id]==0) now++;
cnt[a[i].id]++;
que[++tt]=i;
while(hh<=tt&&cnt[a[que[hh]].id]>1) cnt[a[que[hh++]].id]--;
if(now==k) ans=min(ans,a[i].x-a[que[hh]].x);
}
write(ans);
return 0;
}