poj 1456.Supermarket

来源:互联网 发布:淘宝凤求凰cdk抽奖 编辑:程序博客网 时间:2024/05/22 04:40

Supermarket

 #include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 10010int pre[N];struct node{int profit, deadline;}p[N];bool operator < (const node& a, const node& b){return a.profit > b.profit;}int find(int x){int root, temp;root = x;while(root != pre[root])root = pre[root];while(x != root){temp = pre[x];pre[temp] = root;x = temp;}return root;}int main(){int num;int root;int maxprofit;while(~scanf("%d", &num)){maxprofit = 0;for(int i = 1; i < N; ++i)pre[i] = i;for(int i = 0; i < num; ++i)scanf("%d%d", &p[i].profit, &p[i].deadline);sort(p, p + num);for(int i = 0; i < num; ++i){root = find(p[i].deadline);if(root > 0){pre[root] = root - 1;maxprofit += p[i].profit;}}printf("%d\n", maxprofit);}return 0;}                


 

原创粉丝点击