/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 324.0 KiB
#3 Accepted 50ms 744.0 KiB
#4 Accepted 68ms 2.652 MiB
#5 Accepted 102ms 5.07 MiB
#6 Accepted 99ms 3.543 MiB
#7 Accepted 97ms 664.0 KiB
#8 Accepted 107ms 1.129 MiB
#9 Accepted 45ms 3.27 MiB
#10 Accepted 60ms 584.0 KiB
#11 Accepted 38ms 1.305 MiB

Code

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
using namespace std;

void solve(int cs) {
  int n, k;
  cin >> n >> k;
  map<int,int> cnt;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    cnt[x] += 1;
  }
  multiset<int> S;
  for (auto &it : cnt) S.insert(it.second);
  vector<int> cc(k, n / k);
  for (int i = 0; i < (n % k); i++) {
    cc[i] += 1;
  }
  multiset<int> need;
  for (auto &it : cc) need.insert(it);
  int ans = 0;
  while (need.size()) {
    int have = *S.rbegin();
    S.erase(--S.end());
    int del = *need.rbegin();
    need.erase(--need.end());
    if (have > del) {
      S.insert(have - del);
    } else if (have < del) {
      del -= have;
      ans += del;
      while (del > 0) {
        int small = *S.begin();
        S.erase(S.begin());
        if (small > del) {
          S.insert(small - del);
          break;
        } else if (small == del) {
          break;
        } else {
          del -= small;
        }
      }
    }
  }
  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc = 1;
  cin >> tc;
  for (int cs = 1; cs <= tc; cs++) {
    solve(cs);
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1062 Roy and Array
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-15 03:29:55
Judged At
2024-11-11 03:21:57
Judged By
Score
100
Total Time
107ms
Peak Memory
5.07 MiB