fork download
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int main() {
  8. int t;
  9. cin >> t; // Number of test cases
  10.  
  11. while (t--) {
  12. int n, k;
  13. cin >> n >> k; // Number of cards and exchange rate
  14.  
  15. vector<int> cards(n);
  16. unordered_map<int, int> frequency; // To store frequency of each number
  17.  
  18. // Taking input for numbers on the cards and counting frequency
  19. for (int i = 0; i < n; ++i) {
  20. cin >> cards[i];
  21. frequency[cards[i]]++;
  22. }
  23.  
  24. int removed_cards = 0;
  25.  
  26. // Iterate over each frequency to determine cards removed by exchanges
  27. for (auto& pair : frequency) {
  28. // Calculate how many full sets of k cards can be made for this number
  29. int full_sets = pair.second / k;
  30. // Update total removed cards
  31. removed_cards += full_sets * k;
  32. }
  33.  
  34. // Calculate the remaining cards after all possible exchanges
  35. int remaining_cards = n - removed_cards;
  36.  
  37. // Check for cards with counts less than k
  38. for (auto& pair : frequency) {
  39. if (pair.second < k && pair.second > 0) {
  40. remaining_cards += pair.second;
  41. break;
  42. }
  43. }
  44.  
  45. cout << remaining_cards << endl;
  46. }
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0.01s 5296KB
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
4
2
6
2
6
3
7