Codeforces Round #604 (Div. 2) C. Beautiful Regional Contest(贪心)

题目链接:https://codeforces.com/contest/1265/problem/C

题意

从大到小给出 $n$ 只队伍的过题数,要颁发 $g$ 枚金牌,$s$ 枚银牌,$b$ 枚铜牌,要求:

  • $g < s, g < b$
  • $g + s + d le lfloor frac{n}{2} floor$
  • 金牌队伍的过题数大于银牌,银牌队伍的过题数大于铜牌

输出颁发奖牌数最多的一种方案。

题解

根据第三个要求,奖牌一定是按过题数的大小颁发的。

金牌只颁发给过题最多数的队伍,可以使 $g < s, g < b$ 最容易满足。

接下来在保证 $g + s + d le lfloor frac{n}{2} floor$ 的前提下使 $g < s, g < b$ 即可。

代码

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

void solve() {
    int n; cin >> n;
    map<int, int> mp;
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        mp[x]++;
    }
    int g = 0, s = 0, b = 0;
    for (auto it = mp.rbegin(); it != mp.rend(); it++) {
        if (g + s + b + (*it).second <= n / 2) {
            if (g == 0)
                g = (*it).second;
            else if (s <= g)
                s += (*it).second;
            else
                b += (*it).second;
        } else break;
    }
    if (g < s and g < b)
        cout << g << ' ' << s << ' ' << b << "
";
    else
        cout << 0 << ' ' << 0 << ' ' << 0 << "
";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}
原文地址:https://www.cnblogs.com/Kanoon/p/13192306.html