传送门
把所有点按坐标排序,用尺取法(双指针)跑一遍就行了。
代码:
#include<bits/stdc++.h>
#define N 1000005
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
int n,k,ans=2e9,tot=0,cnt[70],colors=0;
struct Node{int col,pos;}p[N];
inline bool cmp(Node a,Node b){return a.pos<b.pos;}
int main(){
n=read(),k=read();
for(int i=1;i<=k;++i){
int t=read();
while(t--)p[++tot]=(Node){i,read()};
}
sort(p+1,p+n+1,cmp);
for(int l=1,r=0;l<=n;){
while(colors<k&&r<n){
++r,++cnt[p[r].col];
if(cnt[p[r].col]==1)++colors;
}
while(colors>=k){
ans=min(ans,p[r].pos-p[l].pos);
--cnt[p[l].col];
if(!cnt[p[l].col])--colors;
++l;
}
if(r==n)break;
}
cout<<ans;
return 0;
}