您的位置:首页 > 其它

[luoguP2564][SCOI2009]生日礼物(队列)

2017-05-01 20:55 218 查看

传送门

 

当然可以用队列来搞啦。

# include <iostream>
# include <cstdio>
# include <cstring>
# include <string>
# include <cmath>
# include <vector>
# include <map>
# include <queue>
# include <cstdlib>
# include <algorithm>
# define MAXN 1000001
using namespace std;

inline int get_num() {
int k = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) k = k * 10 + c - '0';
return k * f;
}

int n, k, tot, sum, h, t, ans = 1 << 30;
int num[MAXN];

struct node
{
int a, pos;
}p[MAXN];

inline bool cmp(node x, node y)
{
return x.pos < y.pos;
}

int main()
{
int i, j, m;
n = get_num();
k = get_num();
for(i = 1; i <= k; i++)
for(j = 1, m = get_num(); j <= m; j++)
p[++tot].a = i, p[tot].pos = get_num();
sort(p + 1, p + n + 1, cmp);
for(h = 1, t = 0; h <= n; h++)
{
while(sum < k && t < n)
{
if(!num[p[++t].a]) sum++;
num[p[t].a]++;
}
if(sum == k) ans = min(ans, p[t].pos - p[h].pos);
num[p[h].a]--;
if(!num[p[h].a]) sum--;
}
printf("%d", ans);
return 0;
}
View Code

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: