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. inline int power(int a, int b) {
  7. int x = 1;
  8. while (b) {
  9. if (b & 1) x *= a;
  10. a *= a;
  11. b >>= 1;
  12. }
  13. return x;
  14. }
  15.  
  16.  
  17. const int M = 1000000007;
  18. const int N = 3e5+9;
  19. const int INF = 2e9+1;
  20. const int LINF = 2000000000000000001;
  21.  
  22. //_ ***************************** START Below *******************************
  23.  
  24. vector<int> a;
  25.  
  26. pair<int, vector<int>> consistency(int n){
  27.  
  28. sort(begin(a), end(a));
  29. vector<int> order(n);
  30.  
  31. int j = 0;
  32. for(int i=1; i<n; i+=2){
  33. order[i] = a[j];
  34. j++;
  35. }
  36. for(int i=0; i<n; i+=2){
  37. order[i] = a[j];
  38. j++;
  39. }
  40.  
  41. int ct = 0;
  42. for(int i=1; i<n-1; i+=2){
  43. if(order[i] < order[i-1] && order[i] < order[i+1]) ct++;
  44. }
  45.  
  46.  
  47. return {ct, order };
  48.  
  49. }
  50.  
  51. void solve() {
  52.  
  53. int n;
  54. cin>>n;
  55.  
  56. a.resize(n);
  57. for(int i=0; i<n; i++) cin >> a[i];
  58.  
  59. auto ans = consistency(n);
  60.  
  61. cout << ans.first << endl;
  62. for(auto& it : ans.second){
  63. cout << it << " ";
  64. }cout << endl;
  65.  
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73. int32_t main() {
  74. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  75.  
  76. int t = 1;
  77. // cin >> t;
  78. while (t--) {
  79. solve();
  80. }
  81.  
  82. return 0;
  83. }
Success #stdin #stdout 0.01s 5324KB
stdin

5
1 2 2 4 5
7
1 3 2 2 4 5 4
6
4 1 4 4 4 4 
6
4 1 2 3 2 4
stdout
2
2 1 4 2 5