fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. //12th may atlassian
  5.  
  6. int main()
  7. {
  8. ios::sync_with_stdio(false);
  9. cin.tie(nullptr);
  10.  
  11. int n, d;
  12. cin >> n >> d;
  13. vector<int> v(n);
  14. unordered_map<int, int> freq;
  15. for (int i = 0; i < n; i++)
  16. {
  17. cin >> v[i];
  18. freq[v[i]]++;
  19. }
  20.  
  21. priority_queue<pair<int, int>> pq;
  22. for (auto it : freq)
  23. pq.push({it.second, it.first});
  24.  
  25. queue<pair<int, int>> cooldown;
  26. vector<int> ans;
  27.  
  28. while (!pq.empty())
  29. {
  30. pair<int, int> top = pq.top();
  31. pq.pop();
  32.  
  33. int count = top.first;
  34. int num = top.second;
  35.  
  36. ans.push_back(num);
  37. count--;
  38.  
  39. cooldown.push({count, num});
  40.  
  41. if (cooldown.size() >= d)
  42. {
  43. pair<int, int> front = cooldown.front();
  44. cooldown.pop();
  45. if (front.first > 0)
  46. pq.push(front);
  47. }
  48. }
  49.  
  50. if (ans.size() == n)
  51. {
  52. cout << "Yes" << endl;
  53. for (int x : ans)
  54. cout << x << " ";
  55. cout << endl;
  56. }
  57. else
  58. {
  59. cout << "No" << endl;
  60. }
  61.  
  62. return 0;
  63. }
  64.  
Success #stdin #stdout 0s 5308KB
stdin
5 3
1 2 3 1 2
stdout
Yes
2 1 3 2 1