Codeforces 1490F 枚举+贪心

1490F
题意:给了一串数字,通过从删除任意几个元素是数组中每个元素的个数都相同;
题解:先统计出每个数字的个数,然后枚举保留个数,取得最优解

#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
using namespace std;
const int N = 200010;
#define x first
#define y second
int a[N];
int main() {
    int t;
    cin >> t;
    while (t--) {
        map<int, int> mp;
        set<int>s;
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i], mp[a[i]]++;
        if (mp.size() == n) cout << 0 << endl;///每个数字都只出现1次;
        else {
            for(auto i:mp) s.insert(i.y); //把所有出现的个数存到set里,可以减少枚举次数;
            int ans=0x3f3f3f3f;
            for(auto t:s){
                int k=t;
                int sum=0;
                for(auto i:mp){
                    if(i.y==k) continue;
                    else {
                        if(i.y<k) sum+=i.y;
                        else sum+=(i.y-k);//只能删除;
                    }
                }
                ans=min(ans,sum);
            }
            cout<<ans<<endl;
        }
    }

//
//4 2 4 3 2 2 3 3/
//8
//3 1 2 3 3 4 3 3
//7
//2 3 1 4 1 1 2
//
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值