[CF1490F] Equalize the Array

Description

给出序列问最少需要删除多少个元素,才可以使得剩余的序列中,所有不同数字出现的次数均相等。

Solution

从出现次数的角度考虑,对于一个元素,如果大于等于一个设定值 x,就把他变为 x,否则变为 0,要让最后的总和最大

那么显然我们只需要枚举一下 x,统计大于等于 x 的数的个数 cnt,此时的答案就是 x cnt

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

#define int long long

void solve()
{
    int n;
    cin >> n;

    map<int, int> mp;
    for (int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        mp[x]++;
    }

    multiset<int> ms;
    for (auto [x, y] : mp)
        ms.insert(y);

    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        ms.erase(i - 1);
        ans = max(ans, static_cast<int>(ms.size() * i));
    }
    cout << n - ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);

    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}

相关文章:

  • 2021-08-15
  • 2021-10-19
  • 2021-12-28
  • 2021-07-17
  • 2021-07-26
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-04
  • 2021-08-15
  • 2022-12-23
  • 2022-02-03
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
相关资源
相似解决方案