题解:AT_wtf22_day1_a Save the Monsters

· · 题解

无论如何,英雄都会有 a-b 个操作不被抵消。一个简单的想法是会有 \min(n,a-b) 个怪兽被杀死。但是我们可以保护一些怪兽,使得留给英雄的怪兽数量最小化。

显然,保护一个怪物就必须全程保护它,否则等于没保护。易得贪心:按照怪物被攻击次数(需要保护次数)排序,从小到大保护。

按照上述策略保护怪兽 p_1\dots p_k,假如英雄攻击当中的怪兽就防御;否则判断防御后能否保证 p_1\dots p_k 可被保护,再进行防御。这样答案就是 n-\min(k,a-b)。

复杂度 O(n\log n),桶排即可线性。

代码

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

const int N = 3e5 + 5;
int n, m, x, buc[N], a, b;

signed main(){
    cin >> n >> m >> a >> b;
    for(int i = 1; i <= m; ++i) scanf("%d", &x), ++buc[x];
    int cnt = 0;
    sort(buc + 1, buc + 1 + n);
    for(int i = 1, s = 0; i <= n; ++i){
        s += buc[i];
        if(s > b) break;
        ++cnt;
    }
    cout << n - min(n - cnt, a - b);
    return 0;
}