cf题目链接
题意简述:给定一序列,现在有一操作:每次删去两个不同的数,问最少可以剩下几个数
解析:emm,没什么解析的,最终剩下的数的数量最小,我们每次都删去第一多的数和第二多的数即可,请自行思考(doge):最终只会剩下0种数或1种数,分别想想
AcCode
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#define inf 0x3f3f3f3f
#define int long long
using namespace std;
const int N = 1e5 + 100;
signed main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
map<int, int> mp;//数最大为1e9,开map记录数量
for (int i = 1; i <= n; i++) {
int value;
cin >> value;
if (mp.count(value)) mp[value]++;
else mp[value] = 1;
}
priority_queue<int, vector<int>, less<int>> q;//优先队列,从大到下弹出
map<int, int>::iterator iter = mp.begin();
for (iter; iter != mp.end(); ++iter) {
q.push((*iter).second);
}
while (q.size() > 1) {
int fir = q.top(); q.pop();
int sec = q.top(); q.pop();
--fir;
--sec;
if (fir)q.push(fir);
if(sec) q.push(sec);
}
if (q.empty()) cout << 0 << endl;
else cout << q.top() << endl;
}
}