您的位置:首页 > 其它

[bzoj1293][SCOI2009]生日礼物(莫队)

2017-09-29 17:02 411 查看

题目:

我是超链接

题解:

枚举起点,每次加入点的时候加到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
;
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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: