12.17省選模擬t1 生日禮物
阿新 • • 發佈:2021-12-24
題目
連結: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; }