fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b) {
  14. int x = 1;
  15. a %= M;
  16. while (b) {
  17. if (b & 1) x = (x * a) % M;
  18. a = (a * a) % M;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31.  
  32. vector<vector<int>> consistency(int n, int m){
  33.  
  34. vector<int> freq(m + 1, 0);
  35. vector<int> ans(a);
  36.  
  37. for (int i = 0; i < n; i++) {
  38. if (a[i] <= m) {
  39. freq[a[i]]++;
  40. }
  41. }
  42.  
  43. int target = n / m;
  44.  
  45. vector<int> mark;
  46. for(int i=0; i<n; i++){
  47. if(a[i]>m) mark.push_back(i);
  48. else {
  49. if(freq[a[i]] > target){
  50. mark.push_back(i);
  51. freq[a[i]]--;
  52. }
  53. }
  54. }
  55.  
  56.  
  57. int changes = 0;
  58.  
  59. for (int i = 1; i <= m; i++) {
  60. while(freq[i] < target){
  61. int j = mark.back();
  62. mark.pop_back();
  63.  
  64. ans[j] = i;
  65. freq[i]++;
  66.  
  67. changes++;
  68. }
  69. }
  70.  
  71. return {{target, changes}, ans};
  72.  
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. vector<vector<int>> practice(int n, int m){
  84.  
  85.  
  86. return {};
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93. void solve() {
  94.  
  95. int n, m;
  96. cin>> n >> m;
  97.  
  98. a.resize(n);
  99. for(int i=0; i<n; i++) cin >> a[i];
  100.  
  101. auto ans = consistency(n, m);
  102.  
  103. cout << ans[0][0] << " " << ans[0][1] << endl;
  104. for(auto& it : ans[1]){
  105. cout << it << " ";
  106. }cout << endl;
  107.  
  108.  
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115. int32_t main() {
  116. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  117.  
  118. int t = 1;
  119. // cin >> t;
  120. while (t--) {
  121. solve();
  122. }
  123.  
  124. return 0;
  125. }
Success #stdin #stdout 0.01s 5324KB
stdin
7 3
1 3 2 2 2 2 1
stdout
2 1
1 3 2 3 2 2 1