您的位置:首页 > 其它

[POJ1456]Supermarket(贪心 + 优先队列 || 并查集)

2017-06-15 19:06 381 查看

传送门

 

1.贪心 + 优先队列

按照时间排序从前往后

很简单不多说

——代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#define N 10001

int n, ans;
int f
;

struct node
{
int a, b;
}p
;

inline bool cmp(node x, node y)
{
return x.a > y.a;
}

inline int find(int x)
{
return x == f[x] ? x : f[x] = find(f[x]);
}

inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
}

int main()
{
int i, x;
while(~scanf("%d", &n))
{
ans = 0;
for(i = 1; i < N; i++) f[i] = i;
for(i = 1; i <= n; i++) p[i].a = read(), p[i].b = read();
std::sort(p + 1, p + n + 1, cmp);
for(i = 1; i <= n; i++)
{
x = find(p[i].b);
if(x)
{
f[x] = x - 1;
ans += p[i].a;
}
}
printf("%d\n", ans);
}
return 0;
}
View Code

 

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