Codeforces Global Round 19 E. Best Pair
的取值种类数不超过。因此我们可以枚举 然后贪心选最大的值。
#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;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
2021-10-02 luogu题目选做(一)