fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int sets, gifts, number;
  10.  
  11. cin >> sets;
  12.  
  13. for (int i = 0; i < sets; i++)
  14. {
  15. cin >> gifts;
  16.  
  17. vector<int> allGifts;
  18.  
  19. for (int y = 0; y < gifts; y++)
  20. {
  21. cin >> number;
  22.  
  23. allGifts.push_back(number);
  24. }
  25.  
  26. map<int, int> countMap;
  27.  
  28. for (auto& elem : allGifts)
  29. {
  30. auto result = countMap.insert(pair<int, int>(elem, 1));
  31.  
  32. if (result.second == false)
  33. {
  34. result.first->second++;
  35. }
  36. }
  37.  
  38. vector<int> toReplace;
  39.  
  40. for (auto& elem : countMap)
  41. {
  42.  
  43. if (elem.second > 1)
  44. {
  45.  
  46. auto found = find(begin(allGifts), end(allGifts), elem.first);
  47.  
  48. auto position = distance(begin(allGifts), found);
  49.  
  50. auto position2 = position;
  51.  
  52.  
  53. for (int h = 0; h < elem.second - 1; h++)
  54. {
  55. auto found2 = find(allGifts.begin() + position2 + 1, end(allGifts), elem.first);
  56.  
  57. position2 = distance(begin(allGifts), found2);
  58.  
  59. toReplace.push_back(position2);
  60.  
  61. }
  62.  
  63. }
  64. }
  65.  
  66. sort(toReplace.begin(), toReplace.end());
  67.  
  68.  
  69. for (int j = 0; j < toReplace.size(); j++)
  70. {
  71. int n = 1;
  72.  
  73. while (n)
  74. {
  75. if (any_of(allGifts.begin(), allGifts.end(), [n](int i) {return i == n; }) == false)
  76. {
  77.  
  78. break;
  79. }
  80. else
  81. {
  82. n++;
  83. }
  84. }
  85.  
  86. int where = toReplace[j];
  87.  
  88. swap(n, allGifts[where]);
  89.  
  90. }
  91.  
  92. if (toReplace.empty() == true)
  93. {
  94. cout << "OK" << endl;
  95. }
  96. else
  97. {
  98. for (int n : allGifts)
  99. {
  100. cout << n << " ";
  101. }
  102.  
  103. cout << endl;
  104. }
  105.  
  106. }
  107. return 0;
  108. }
Success #stdin #stdout 0s 4568KB
stdin
Standard input is empty
stdout
Standard output is empty