F. Equalize the Array

来源:Codeforces #702(div 3)
https://codeforces.com/contest/1490/problem/F

题意:
给定一个长度为n的数组,问最少删除多少个数,使得新数组中每个数出现的次数相同。

题解:
记录每个数出现的次数,然后存入 n u m num num中,按从大到小排序,那么每个次数需要保留的元素个数就是 n u m [ i ] ∗ ( i + 1 ) num[i]*(i+1) num[i](i+1),也就是,所有出现次数大于等于当前次数的元素个数 × 当前出现次数。将num里的每个元素枚举一遍,找出最大值ans,那么最终最少需要删除的元素个数就是总个数n - ans。

具体细节见代码:

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

map<int, int> num;
std::vector<int> all;
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n;
        num.clear();
        all.clear();
        cin >> n;
        for (int i = 0; i < n; i++) {
            int x;
            cin >> x;
            num[x]++;
        }
        for (auto k : num) {
            all.emplace_back(k.second);
        }
        sort(all.begin(), all.end(), greater<int>());
        int ans = 0;
        int len = all.size();
        for (int i = 0; i < len; i++) {
            ans = max(ans, all[i] * (i + 1));
        }
        cout << n - ans << endl;
    }
    return 0;
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值