题解:P16460 [UOI 2026] Minimum Deletion

· · 题解

题解:P16460 [UOI 2026] Minimum Deletion

思路

贪心地想,既然我们必须要让小于等于 k 的非负整数至少出现一个,那我们肯定优先删那个出现次数最少的数。

我们可以记录数组中每一个数出现的次数,然后从 0 到 k 遍历取出出现次数最少的元素,这个元素就是答案。

AC code

#include <iostream>
using namespace std;
#define endl '\n'
// #define int long long
int n, k, a[20], ans = 1e9;
signed main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> k;
    while(n--){
        int tmp;
        cin >> tmp;
        a[tmp]++;
    }
    for(int i = 0; i <= k; i++)
        ans = min(a[i], ans);
    cout << ans;
}