fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4.  
  5. using namespace std;
  6.  
  7. int minCardsLeft(int n, int k, vector<int>& cards) {
  8. unordered_map<int, int> freq;
  9. for (int card : cards) {
  10. freq[card]++;
  11. }
  12.  
  13. int minCards = 0;
  14. for (auto it = freq.begin(); it != freq.end(); ++it) {
  15. int num = it->first;
  16. int count = it->second;
  17. while (count >= k) {
  18. minCards += k;
  19. count -= (k - 1);
  20. }
  21. minCards += count;
  22. }
  23. return n - minCards;
  24. }
  25.  
  26. int main() {
  27. int t;
  28. cin >> t;
  29.  
  30. while (t--) {
  31. int n, k;
  32. cin >> n >> k;
  33. vector<int> cards(n);
  34. for (int i = 0; i < n; ++i) {
  35. cin >> cards[i];
  36. }
  37. cout << minCardsLeft(n, k, cards) << endl;
  38. }
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 5316KB
stdin
7
5 3
4 1 1 4 4
1 10
7
7 2
4 2 1 100 5 2 3
10 4
1 1 1 1 1 1 1 1 1 1
5 2
3 8 1 48 7
6 2
10 20 30 10 20 40
6 3
10 20 30 10 20 40
stdout
-1
0
-1
-3
0
-2
0