Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define fixed(n) fixed << setprecision(n)
- #define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
- #define add_mod(a, b, m) (((a % m) + (b % m)) % m)
- #define sub_mod(a, b, m) (((a % m) - (b % m) + m) % m)
- #define mul_mod(a, b, m) (((a % m) * (b % m)) % m)
- #define all(vec) vec.begin(), vec.end()
- #define rall(vec) vec.rbegin(), vec.rend()
- #define sz(x) int(x.size())
- #define debug(x) cout << #x << ": " << (x) << "\n";
- #define fi first
- #define se second
- #define ll long long
- #define ull unsigned long long
- #define Mod 1'000'000'007
- #define EPS 1e-9
- constexpr int INF = 1 << 30;
- constexpr ll LINF = 1LL << 62;
- #define PI acos(-1)
- template < typename T = int > using Pair = pair < T, T >;
- vector < string > RET = {"NO", "YES"};
- template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
- for (auto &x : v) in >> x;
- return in;
- }
- template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
- for (const T &x : v) out << x << ' ';
- return out;
- }
- void Solve(){
- int n;
- cin >> n;
- set < int > received;
- vector < int > ans(n);
- for(int i = 1; i <= n; i++)
- received.insert(i);
- for(int i = 0, x; i < n && cin >> x; i++){
- if(x > 0)
- received.erase(x);
- ans[i] = x;
- }
- int last_received = -1;
- for(int i = 0; i < n; i++){
- if(ans[i] == 0){
- if(i + 1 == *received.rbegin())
- ans[i] = *received.begin();
- else
- ans[i] = *received.rbegin();
- if(ans[i] == i + 1){
- swap(ans[i], ans[last_received]);
- break;
- }
- received.erase(ans[i]);
- last_received = i;
- }
- }
- cout << ans << '\n';
- }
- int main(){
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- int test_cases = 1;
- // cin >> test_cases;
- for(int tc = 1; tc <= test_cases; tc++){
- // cout << "Case #" << tc << ": ";
- Solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement