您的位置:首页 > 其它

bzoj1293: [SCOI2009]生日礼物

2017-09-13 14:16 197 查看
考虑双指针,显然区间中必须有一种颜色一个,所以如果队首的元素在区间中出现过两次以上,就可以将它弹出。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int read()
{
char ch=getchar();int f=0;
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9'){f=(f<<1)+(f<<3)+ch-'0';ch=getchar();}
return f;
}
int sum[65],n,k,tot,num,ans=0x7fffffff;
struct data
{
int col;
int pla;
}a[1000005];
int cmp(const data &x,const data &y)
{
return x.pla<y.pla;
}
int main()
{
n=read();k=read();
for(int i=1;i<=k;i++)
{
int m=read();
for(int j=1;j<=m;j++)
{
int x=read();
a[++tot].col=i;
a[tot].pla=x;
}
}
sort(a+1,a+n+1,cmp);
int l=1;
for(int i=1;i<=n;i++)
{
if(!sum[a[i].col])
num++;
sum[a[i].col]++;
while(sum[a[l].col]!=1)
{
sum[a[l].col]--;
l++;
}
if(num==k)
ans=min(ans,a[i].pla-a[l].pla);
}
//cout<<num;
cout<<ans;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: