标签:
Description
Input
Output
Sample Input
4 50 2 10 1 20 2 30 1 7 20 1 2 1 10 3 100 2 8 2 5 20 50 10
Sample Output
80 185
#include<stdio.h> #include<stdlib.h> #define N 10010 int f[N]; struct node { int p, d; } no[N]; void Init() { int i; for (i = 0; i < N; i++) f[i] = i; } int cmp(const void *a, const void *b) { node *s1 = (node *)a, *s2 = (node *)b; if (s1->p != s2->p) return s2->p - s1->p; return s1->d - s2->d; } int Find(int x) { if (f[x] != x) f[x] = Find(f[x]); return f[x]; } int main () { int n, i, ans, x; while (scanf("%d", &n) != EOF) { Init(); ans = 0; for (i = 0; i < n; i++) scanf("%d %d", &no[i].p, &no[i].d); qsort(no, n, sizeof(no[0]), cmp); for (i = 0; i < n; i++) { x = Find(no[i].d); if (x != 0) //如果等于0,则表示这个时间点之前都卖过了东西,那么这个东西就不能卖了 { ans += no[i].p; f[x] = x-1; } } printf("%d\n", ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/syhandll/p/4683538.html