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, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27. // 7
  28. // 1 3 4 1 2 3 1
  29.  
  30.  
  31. vector<int> a;
  32.  
  33. //* Ammoritzed O(N)
  34.  
  35. vector<vector<int>> consistency1(int n){
  36. unordered_map<int,int> mp;
  37.  
  38. for(int i=0; i<n; i++){
  39. mp[a[i]]++;
  40.  
  41. }
  42.  
  43.  
  44. vector<vector<int>> ans;
  45.  
  46. for(auto& it : mp){
  47. int f = it.second;
  48. int x = it.first;
  49.  
  50.  
  51. int m = ans.size();
  52. int i = 0;
  53.  
  54. while(i<min(m, f)){
  55.  
  56. ans[i].push_back(x);
  57. i++;
  58. }
  59.  
  60.  
  61. while(i<f){
  62. ans.push_back({x});
  63. i++;
  64. }
  65. }
  66.  
  67.  
  68. return ans;
  69.  
  70. }
  71.  
  72.  
  73.  
  74. //* Optimized O(N)
  75.  
  76. vector<vector<int>> consistency2(int n){
  77.  
  78. vector<vector<int>> ans;
  79. unordered_map<int,int> mp;
  80.  
  81. for(int i=0; i<n; i++){
  82. mp[a[i]]++;
  83. int f = mp[a[i]];
  84.  
  85. if(f-1 >= ans.size()){
  86. ans.push_back({a[i]});
  87. }
  88. else{
  89. ans[f-1].push_back(a[i]);
  90. }
  91.  
  92. }
  93.  
  94.  
  95. return ans;
  96.  
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. vector<vector<int>> practice(int n){
  110.  
  111.  
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118. void solve() {
  119.  
  120. int n;
  121. cin>> n;
  122.  
  123. a.resize(n);
  124. for(int i=0; i<n; i++) cin >> a[i];
  125.  
  126. auto ans1 = consistency1(n);
  127.  
  128. for(int i = 0; i<ans1.size(); i++){
  129. sort(begin(ans1[i]), end(ans1[i]));
  130. for(auto& t : ans1[i]) cout << t << " ";
  131. cout << endl;
  132. }
  133.  
  134. auto ans2 = consistency2(n);
  135.  
  136. for(int i = 0; i<ans2.size(); i++){
  137. sort(begin(ans2[i]), end(ans2[i]));
  138. for(auto& t : ans2[i]) cout << t << " ";
  139. cout << endl;
  140. }
  141.  
  142.  
  143. }
  144.  
  145.  
  146.  
  147.  
  148.  
  149. int32_t main() {
  150. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  151.  
  152. int t = 1;
  153. // cin >> t;
  154. while (t--) {
  155. solve();
  156. }
  157.  
  158. return 0;
  159. }
Success #stdin #stdout 0s 5296KB
stdin
7
1 3 4 1 2 3 1
stdout
1 2 3 4 
1 3 
1 
1 2 3 4 
1 3 
1