0
点赞
收藏
分享

微信扫一扫

D. Epic Transformation


// Problem: D. Epic Transformation
// Contest: Codeforces - Codeforces Round #710 (Div. 3)
// URL: https://codeforces.com/problemset/problem/1506/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 2022-02-21 20:56:47
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,l,r) for(int i=(l);i>=(r);i--)
#define ll long long
#define pii pair<int, int>
#define mset(s,t) memset(s,t,sizeof(t))
#define mcpy(s,t) memcpy(s,t,sizeof(t))
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define mp make_pair

const ll mod = 1e9 + 7;

inline int read () {
int x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) f |= (ch=='-'),ch= getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return f?-x:x;
}
template<typename T> void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x/10);
putchar(x % 10 + '0');
}
void solve() {
int n;
cin >> n;
priority_queue<pii> heap;
map<int, int> cnt;
for (int i = 0; i < n; i ++) {
int t;
cin >> t;
cnt[t] ++;
}
for (auto [t, ct]: cnt)
heap.push(mp(ct, t));
int ans = n;
while (heap.size() > 1) {
auto a = heap.top(); heap.pop();
auto b = heap.top(); heap.pop();
ans -= 2;
if (a.fi - 1 >0)
heap.push(mp(a.fi - 1,a.se));
if (b.fi - 1 >0)
heap.push(mp(b.fi - 1, b.se));
}
cout << ans << endl;
}
int main () {
int t;
t =1;
cin >> t;
while (t --) solve();
return 0;
}


每次选则出现次数最大和次大的两个数进行配对。然后就能取得最大值


举报

相关推荐

0 条评论