D. Epic Transformation

网友投稿 569 2022-12-01

D. Epic Transformation

D. Epic Transformation

// Problem: D. Epic Transformation// Contest: Codeforces - Codeforces Round #710 (Div. 3)// URL: Memory Limit: 256 MB// Time Limit: 2000 ms// 2022-02-21 20:56:47// // Powered by CP Editor (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#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_pairconst 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 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 heap; map 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-(); heap.pop(); auto b = heap-(); 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;}

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:1547D - Co-growing Sequence
下一篇:JPA之映射mysql text类型的问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~