/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 1ms 320.0 KiB
#3 Accepted 69ms 568.0 KiB
#4 Accepted 71ms 2.398 MiB
#5 Accepted 120ms 5.059 MiB
#6 Accepted 114ms 4.695 MiB
#7 Accepted 96ms 368.0 KiB
#8 Accepted 105ms 1.016 MiB
#9 Accepted 46ms 2.633 MiB
#10 Accepted 64ms 576.0 KiB
#11 Accepted 36ms 1.215 MiB

Code

#include <bits/stdc++.h>
#define ll long long
#define endll '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int mod = 1e9 + 7, N = 1e6;

void solve()
{
    int n, k;
    cin >> n >> k;
    int ar[n + 3];
    int need[k + 3];
    map<int, int> mp;
    multiset<int, greater<>> st;
    for (int i = 1; i <= n; i++)
        cin >> ar[i], mp[ar[i]]++;
    for (auto i : mp)
    {
        st.insert(i.second);
    }

    // sort(all(v), greater<>());
    int ps = 1;
    for (int i = 1; i <= n; i++)
    {
        if (i <= k)
        {
            need[i] = 1;
        }
        else
        {
            need[ps] += 1;
            ps++;
            if (ps > k)
                ps = 1;
        }
    }

    vector<int> v;
    for (int i = 1; i <= k; i++)
    {
        v.pb(need[i]);
    }

    sort(all(v), greater<>());

int ans = 0;
    for (int i = 0; i < k; i++)
    {

        // cout << v[i] << " = need" << endll;
        int x = *st.begin();
        st.erase(st.begin());
        if(v[i] <=    x) {
            x -= v[i];
            st.insert(x);
        }
        else {
            ans += v[i] - x;
            
        }

    }
    cout << ans << endl;
    // cout << endll;
    // cout << endll;
}

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1062 Roy and Array
Contest
Brain Booster #4
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-14 17:51:31
Judged At
2024-11-11 03:22:39
Judged By
Score
100
Total Time
120ms
Peak Memory
5.059 MiB