Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <cstdlib>
- #include <algorithm>
- #include <cmath>
- #include <iomanip>
- #include <numeric>
- #include <vector>
- #include <string>
- #include <set>
- #include <map>
- #include <deque>
- using namespace std;
- #define ll long long
- void sherry()
- {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- #endif
- }
- /*bool comp(pair<int, int> a, pair<int, int> b)
- {
- return a.second < b.second;
- }*/
- int main()
- {
- sherry();
- ll n, count = 0;
- cin >> n;
- vector<int> v(0);
- deque<int> de;
- vector<int> fre(n + 1);
- for (ll i = 0; i < n; i++)
- {
- ll num;
- cin >> num;
- fre[num]++;
- v.push_back(num);
- }
- for (ll i = 1; i <= n; i++)
- {
- if (fre[i] == 0)
- {
- if (v[i - 1] == 0 && !de.empty())
- {
- v[i - 1] = de.back();
- de.pop_back();
- }
- de.push_back(i);
- }
- }
- for (ll i = 0; i < n; i++)
- {
- if (v[i] == 0)
- {
- if (de.back() == i + 1)
- {
- cout << de.front() << " ";
- de.pop_front();
- }
- else
- {
- cout << de.back() << " ";
- de.pop_back();
- }
- }
- else
- {
- cout << v[i] << " ";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement