Codeforces Global Round 19 E. Best Pair

cnt的取值种类数不超过n。因此我们可以枚举cnt 然后贪心选最大的值。

#include <bits/stdc++.h> using namespace std; using i32 = int32_t; using i64 = long long; #define int i64 using vi = vector<int>; using pii = pair<int,int>; void solve() { int n, m; cin >> n >> m; map<int,int> cnt; for(int i = 1, x; i <= n; i ++) cin >> x, cnt[x] ++; map<int,vi> C; for(const auto &[x, y]: cnt) C[y].push_back(x); for(auto &[x, y] : C) ranges::sort(y, greater<>()); set<pii> vis; for(int x, y; m; m --) { cin >> x >> y; vis.emplace(x, y); } int res = 0; for(const auto &[x , y] : C) { if(y.size() < 2) continue; int ret = 0; for(const auto &i : y) for(const auto &j: y){ if(j <= i) break; if(i + j <= ret) break; if(vis.count({i, j})) continue; ret = max(ret, i + j); res = max(res , (x + x) * (i + j)); } } for(const auto &[a, x] : C) for(const auto &[b , y] : C){ if(b >= a) break; int ret = 0; for(const auto &i : x) for(const auto &j : y) { if(i + j <= ret) break; if(i < j) { if(vis.count({i, j})) continue; } else { if(vis.count({j, i})) continue; } ret = max(ret, i + j); res = max(res, (a + b) * (i + j)); } } cout << res << "\n"; return; } i32 main(){ ios::sync_with_stdio(false), cin.tie(nullptr); int T; cin >> T; while(T --) solve(); return 0; }

__EOF__

本文作者PHarr
本文链接https://www.cnblogs.com/PHarr/p/18444942.html
关于博主:前OIer,SMUer
版权声明CC BY-NC 4.0
声援博主:如果这篇文章对您有帮助,不妨给我点个赞
posted @   PHarr  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-10-02 luogu题目选做(一)
点击右上角即可分享
微信分享提示