题解:AT_abc447_g [ABC447G] Div. 1 & Div. 2
xiaoyiming123 · · 题解
我遇见过的最简单的 ABC G。
Sol
首先,称
结论:
存在最优解使得只有
证明:
随意选取一个最优解,如果满足只有
因为
因此,只需取
枚举量
Code
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> v[100005];
int p[100005];
struct Obj {
int i, k, a;
} o[100005];
int main() {
ios::sync_with_stdio(false);
int n, tot = 0, tot2 = 0;
long long res = -1;
cin >> n;
for (int i = 1, k, a; i <= n; i ++) {
cin >> k >> a;
v[k].push_back({a, i});
}
for (int i = 1; i <= n; i ++) {
if (v[i].size()) {
sort(v[i].begin(), v[i].end(), greater<pair<int, int>>());
p[++ tot] = i;
}
}
sort(p + 1, p + tot + 1, [](int x, int y) {
return v[x][0].first > v[y][0].first;
});
for (int i = 1; i <= tot && i <= 6; i ++) {
for (int j = 0; j < 6 && j < (int)v[p[i]].size(); j ++) {
o[++ tot2] = {v[p[i]][j].second, p[i], v[p[i]][j].first};
}
}
sort(o + 1, o + tot2 + 1, [](Obj x, Obj y) {
return x.i < y.i;
});
for (int a = 1; a <= tot2; a ++) {
for (int b = a + 1; b <= tot2; b ++) {
if (o[b].k == o[a].k) continue;
for (int c = b + 1; c <= tot2; c ++) {
if (o[c].k == o[a].k || o[c].k == o[b].k) continue;
for (int d = c + 1; d <= tot2; d ++) {
if (o[d].k == o[a].k || o[d].k == o[b].k || o[d].k == o[c].k) continue;
for (int e = d + 1; e <= tot2; e ++) {
if (o[e].k == o[c].k || o[e].k == o[d].k) continue;
for (int f = e + 1; f <= tot2; f ++) {
if (o[f].k == o[c].k || o[f].k == o[d].k || o[f].k == o[e].k) continue;
res = max(res, (long long)o[a].a + o[b].a + o[c].a + o[d].a + o[e].a + o[f].a);
}
}
}
}
}
}
cout << res << '\n';
}