题目:
题解:
枚举起点,每次加入点的时候加到k种。因为一个位置上会有好多珠子,你可以按照珠子移动而不是按照颜色移动;
代码:
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#define INF 1<<30
#define N 1000005
using namespace std;
struct hh{int col,pos;}a[N];
int c[100];
int cmp(hh a,hh b){return a.pos<b.pos;}
int main()
{
int n,k,i,j,num=0,cnt=0;
scanf("%d%d",&n,&k);
for (i=1;i<=k;i++)
{
int lo,x;
scanf("%d",&lo);++num;
for (j=1;j<=lo;j++)
{
scanf("%d",&x);
a[++cnt].col=num; a[cnt].pos=x;
}
}
sort(a+1,a+1+n,cmp);
int l=1,r=0,now=0,ans=INF;
while (l<=n)
{
while (now<k && r<n)
{
r++; if (c[a[r].col]==0) now++;
c[a[r].col]++;
}
if (now>=k) ans=min(ans,a[r].pos-a[l].pos);
c[a[l].col]--;if (!c[a[l].col]) now--; l++;
}
printf("%d",ans);
}