思路:我们删的策略是从次数多的数开始删,每次取两种不同的数,每种删去一个,然后放回堆中。
代码:
void solve(){int n;cin >> n;map<int,int>mp;for(int i = 1;i <= n;i ++){int x;cin >> x;mp[x] ++;}int sz = 0;priority_queue<int>q;for(auto t:mp){q.push(t.second);sz += t.second;}while(q.size() > 1){auto t1 = q.top();q.pop();auto t2 = q.top();q.pop();t1 --;t2 --;sz -= 2;if(t1 > 0)q.push(t1);if(t2 > 0)q.push(t2);}cout << sz << endl;
}