fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5.  
  6.  
  7. const int M = 1000000007;
  8. const int N = 3e5+9;
  9. const int INF = 2e9+1;
  10. const int MAXN = 100000;
  11. const int LINF = 2000000000000000001;
  12.  
  13. //_ ***************************** START Below *******************************
  14.  
  15.  
  16.  
  17. vector<int> a;
  18.  
  19. vector<int> consistency(int n ){
  20. vector<int> ans(a);
  21.  
  22. int s = 0, e = 0;
  23. while(e<n & ans[e] != 0){
  24. s++;
  25. e++;
  26. }
  27. while(e<n){
  28.  
  29. if(a[e] != 0){
  30. swap(ans[e], ans[s]);
  31. s++;
  32. }
  33. e++;
  34. }
  35.  
  36. return ans;
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. vector<int> practice(int n){
  48.  
  49. return {};
  50. }
  51.  
  52.  
  53.  
  54.  
  55. void solve() {
  56.  
  57. int n;
  58. cin >> n;
  59. a.resize(n);
  60. for(int i=0; i<n; i++) cin >> a[i];
  61.  
  62. auto ans = consistency(n);
  63. for(auto& it : ans) cout << it << " "; cout << endl;
  64.  
  65. // auto p = practice(n);
  66. // cout << "Practice : ";
  67. // for(auto& it : p) cout << it << " "; cout << endl;
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75. int32_t main() {
  76. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  77.  
  78. int t = 1;
  79. while (t--) {
  80. solve();
  81. }
  82.  
  83. return 0;
  84. }
Success #stdin #stdout 0s 5324KB
stdin
8
1 2 0 4 3 0 5 0
stdout
1 2 4 3 5 0 0 0