题解:P16494 樱蕊初含雪,惜春时易逝

· · 题解

Solution

设三种颜色中最大数量为 c = \max(x,y,z),将 a 升序排序,总和为 sum。

对于任意颜色分配,最大配对总美观度的最优值为:

当 2c > n 时,要使 sub 最大,应让最大的 c 个数作为颜色最多的那类花。
因此直接按上述公式计算即可。

::::success[AC Code]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--) {
        int n, x, y, z;
        cin >> n >> x >> y >> z;
        vector<ll> a(n);
        for (int i = 0; i < n; ++i){
            cin >> a[i];
        }
        sort(a.begin(), a.end());
        ll sum = 0;
        for (ll v : a) sum += v;
        ll c = max({x, y, z});
        ll ans;
        if (2 * c > n) {
            ll k = 2 * c - n;
            ll sub = 0;
            for (int i = n - c; i < n - c + k; ++i) sub += a[i];
            ans = sum - sub;
        }
        else {
            if (n & 1) ans = sum - a[0];
            else ans = sum;
        }
        cout << ans << "\n";
    }
    return 0;
}

::::