求做法正确性
查看原帖
求做法正确性
965281
1313a楼主2024/10/26 21:27

洛谷民间数据100分,感觉用优先队列的人很少 记录

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n,r[maxn],tong[maxn],ans = 0;
struct node{
	int a,b;
	friend bool operator < (node a,node b){
		return a.b < b.b;
	}
}tmp;
priority_queue<node> q;
int main(){
	int maxr = -INT_MAX;
	cin >> n;
	for(int i = 1;i <= n;i++){
		scanf("%d",&r[i]);
		maxr = max(maxr,r[i]);
		tong[r[i]]++;
	}
	for(int i = 1;i <= maxr;i++){
		tmp.a = i;
		tmp.b = tong[i];
		q.push(tmp);
	}
	while(q.size() - 1){
		int a = 0,b = 0,a1 = 0,b1 = 0;
		a = q.top().a;
		b = q.top().b;
		q.pop();
		if(!q.empty())a1 = q.top().a;
		if(!q.empty())b1 = q.top().b;
		q.pop();
		ans += min(b,b1);
		tmp.b = max(b,b1);
		if(b > b1){
			tmp.a = a;
			q.push(tmp);
		}else{
			tmp.a = a1;
			q.push(tmp);
		}
	}
	cout << n - ans;
	return 0;
}
2024/10/26 21:27
加载中...