跳到正文

题解

题解:AT_abc447_g [ABC447G] Div. 1 & Div. 2

xiaoyiming123约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年3月1日
收录
2026年3月6日
更新
2026年11月10日
篇幅
约 5 分钟

版本

  1. v1当前

我遇见过的最简单的 ABC G。

Sol

首先,称 KiK_i 相同的题目为一组。

结论:

存在最优解使得只有 AiA_i 最大值前 66 大的组里的数会被选择。

证明:

随意选取一个最优解,如果满足只有 AiA_i 最大值前 66 大的组里的数会被选择,则结论成立,否则,一定存在 AiA_i 最大值前 66 大的组中的一组未被选择。设这一组内 AiA_i 最大为 a0a_0。设不在前 66 组的一个题目的 AiA_i 为 a1a_1, 组内最大 AiA_i 为 a2a_2。

因为 a2≤a1≤a0a_2 \le a_1 \le a_0,所以将 a2a_2 换成 a0a_0 仍然最优。

因此,只需取 aia_i 最大的 66 组,每组 AiA_i 最大的 66 个题目,共最多 3636 题,然后枚举即可。

枚举量 C636=1947792C^{36}_6=1947792。

Code

C++
#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';
}
原文
存档评论1 条抓取于