Advertisement
7oSkaaa

X - Friends and Gifts

Nov 6th, 2022
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define fixed(n) fixed << setprecision(n)
  6. #define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  7. #define add_mod(a, b, m) (((a % m) + (b % m)) % m)
  8. #define sub_mod(a, b, m) (((a % m) - (b % m) + m) % m)
  9. #define mul_mod(a, b, m) (((a % m) * (b % m)) % m)
  10. #define all(vec) vec.begin(), vec.end()
  11. #define rall(vec) vec.rbegin(), vec.rend()
  12. #define sz(x) int(x.size())
  13. #define debug(x) cout << #x << ": " << (x) << "\n";
  14. #define fi first
  15. #define se second
  16. #define ll long long
  17. #define ull unsigned long long
  18. #define Mod  1'000'000'007
  19. #define EPS 1e-9
  20. constexpr int INF = 1 << 30;
  21. constexpr ll LINF = 1LL << 62;
  22. #define PI acos(-1)
  23. template < typename T = int > using Pair = pair < T, T >;
  24. vector < string > RET = {"NO", "YES"};
  25.  
  26. template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
  27.     for (auto &x : v) in >> x;
  28.     return in;
  29. }
  30.  
  31. template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
  32.     for (const T &x : v) out << x << ' ';
  33.     return out;
  34. }
  35.  
  36. void Solve(){
  37.     int n;
  38.     cin >> n;
  39.     set < int > received;
  40.     vector < int > ans(n);
  41.     for(int i = 1; i <= n; i++)
  42.         received.insert(i);
  43.     for(int i = 0, x; i < n && cin >> x; i++){
  44.         if(x > 0)
  45.             received.erase(x);
  46.         ans[i] = x;
  47.     }
  48.     int last_received = -1;
  49.     for(int i = 0; i < n; i++){
  50.         if(ans[i] == 0){
  51.             if(i + 1 == *received.rbegin())
  52.                 ans[i] = *received.begin();
  53.             else
  54.                 ans[i] = *received.rbegin();
  55.             if(ans[i] == i + 1){
  56.                 swap(ans[i], ans[last_received]);
  57.                 break;
  58.             }
  59.             received.erase(ans[i]);
  60.             last_received = i;
  61.         }
  62.     }
  63.     cout << ans << '\n';
  64. }
  65.  
  66. int main(){
  67.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  68.     int test_cases = 1;
  69.     // cin >> test_cases;
  70.     for(int tc = 1; tc <= test_cases; tc++){
  71.         // cout << "Case #" << tc << ": ";
  72.         Solve();
  73.     }
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement