并查集的超市问题---溜TM的

三个月前我就错了,现在又错了,我就是个傻******

服了,看图哇

打扰了。。。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#define maxn 23000
using namespace std;
int par[maxn];
int find(int x) {
	if (par[x] != -1) return par[x] = find(par[x]);
	else return x;
}
int n, m;
struct Node {
	int v;
	int t;
	Node(int _v, int _t) :v(_v), t(_t) {}
};
bool operator<(const Node a, const Node b) {
	if (a.v == b.v) return a.t < b.t;
	else return a.v < b.v;
}
priority_queue<Node>que;
int main() {
	while (~scanf("%d", &n)) {
		memset(par, -1, sizeof(par));

		for (int i = 0; i < n; i++) {
			int v, t;
			scanf("%d %d", &v, &t);
			que.push(Node(v, t));
		}
		
		long long cnt = 0;
		
		while (!que.empty()) {
			Node ans = que.top();
			que.pop();
			int t = find(ans.t);
			if (t > 0) {
				cnt += ans.v;
				par[t] = t - 1;
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

  最后附上我老婆(逃)

猜你喜欢

转载自www.cnblogs.com/lesning/p/11390535.html
tm