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