Advertisement
Ahmed_Negm

Untitled

May 4th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<iostream>
  3. #include<cmath>
  4. #include<iomanip>
  5. #include<algorithm>
  6. #include<cstdlib>
  7. #include<cstring>
  8. #include<vector>
  9. #include<utility>
  10. using namespace std;
  11.  
  12. #define ll long long
  13. #define nl '\n'
  14. #define sz(x) int(x.size())
  15. #define all(x) x.begin(),x.end()
  16. #define rall(s)  s.rbegin(), s.rend()
  17. #define getline(s) getline(cin>>ws,s)
  18. #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  19. /*
  20. ███╗░░██╗███████╗░██████╗░███╗░░░███╗
  21. ████╗░██║██╔════╝██╔════╝░████╗░████║
  22. ██╔██╗██║█████╗░░██║░░██╗░██╔████╔██║
  23. ██║╚████║██╔══╝░░██║░░╚██╗██║╚██╔╝██║
  24. ██║░╚███║███████╗╚██████╔╝██║░╚═╝░██║
  25. ╚═╝░░╚══╝╚══════╝░╚═════╝░╚═╝░░░░░╚═╝
  26.  
  27. */
  28.  
  29. void Fast_IO(){
  30.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  31.     #ifndef ONLINE_JUDGE
  32.         freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  33.     #endif
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40. void solve(){
  41.   ll n; cin>>n;
  42.   vector<bool>found(n+5);
  43.   deque<ll>non;
  44.   vector<ll>v;
  45.   for(int i=0; i<n; i++){
  46.       ll x; cin>>x;
  47.       v.push_back(x);
  48.       if(x != 0) found[x]=1;
  49.   }
  50.   for(int i=1; i<=n; i++){
  51.       if(found[i]==0) non.push_back(i);
  52.   }
  53.  
  54.   for(int i=0; i<n; i++){
  55.       if(v[i]==0){
  56.           if(non.back() != i+1){
  57.           cout<<non.back()<<' ';
  58.           non.pop_back();
  59.           }
  60.       else{
  61.           cout<<non.front()<<' ';
  62.           non.pop_front();
  63.       }
  64.       }else cout<<v[i]<<' ';
  65.   }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. }
  74.  
  75. int main(){
  76.     Fast_IO();
  77. int t =1;
  78. //cin>>t;
  79. while(t--){
  80. solve();
  81. }
  82. return 0;
  83. }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement