1 #include2 #include 3 #include 4 #include 5 #include 6 #define max(a, b) ((a) > (b) ? (a) : (b)) 7 8 const int INF = 0x3f3f3f3f; 9 const int MAXN = 10000 + 10;10 const int MAXP = 10000 + 10;11 12 inline void read(int &x)13 {14 x = 0;char ch = getchar(), c = ch;15 while(ch < '0' || ch > '9')c = ch, ch = getchar();16 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();17 if(c == '-') x = -x; 18 }19 20 int fa[MAXP], rank[MAXN], value[MAXN], t[MAXN], cnt[MAXN], n, ans, ma;21 22 int find(int x)23 {24 return fa[x] == x ? x : fa[x] = find(fa[x]);25 }26 27 bool cmp(int a, int b)28 {29 return value[a] > value[b];30 }31 32 int main()33 {34 while(scanf("%d", &n) != EOF)35 {36 ans = ma = 0;37 for(register int i = 1;i <= n;++ i)38 read(value[i]), read(t[i]), cnt[i] = i, ma = max(ma, t[i]);39 for(register int i = 1;i <= ma;++ i)40 fa[i] = i;41 std::sort(cnt + 1, cnt + 1 + n, cmp);42 register int p;43 for(register int i = 1;i <= n;++ i)44 {45 p = cnt[i];46 int tmp = find(t[p]);47 if(tmp != 0) ans += value[p], fa[tmp] = tmp - 1;48 }49 printf("%d\n", ans);50 }51 return 0;52 }
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #define max(a, b) ((a) > (b) ? (a) : (b)) 8 9 const int INF = 0x3f3f3f3f;10 const int MAXN = 10000 + 10;11 const int MAXP = 10000 + 10;12 13 inline void read(int &x)14 {15 x = 0;char ch = getchar(), c = ch;16 while(ch < '0' || ch > '9')c = ch, ch = getchar();17 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();18 if(c == '-') x = -x; 19 }20 21 int value[MAXN], t[MAXN], cnt[MAXN], n, ans, ma;22 23 bool cmp(int a, int b)24 {25 return t[a] > t[b];26 } 27 28 struct cmpp29 {30 bool operator()(int a, int b)31 {32 return value[a] < value[b];33 } 34 };35 36 std::priority_queue