-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1283C.cpp
More file actions
47 lines (39 loc) · 1016 Bytes
/
Copy path1283C.cpp
File metadata and controls
47 lines (39 loc) · 1016 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> arr(n);
vector<int> avail(n, 0);
for (int i = 0; i < n; i++)
avail[i] = i + 1;
vector<int> zeros;
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] == 0)
zeros.push_back(i);
else
avail[arr[i] - 1] = -1;
}
vector<int> av;
for (int x : avail)
if (x != -1) av.push_back(x);
for (int i = 0; i < (int)zeros.size(); i++) {
int pos = zeros[i];
arr[pos] = av[i];
}
for (int i = 0; i < (int)zeros.size(); i++) {
int pos = zeros[i];
if (arr[pos] == pos + 1) {
if (i + 1 < (int)zeros.size())
swap(arr[pos], arr[zeros[i + 1]]);
else
swap(arr[pos], arr[zeros[i - 1]]);
}
}
for (int i = 0; i < n; i++)
cout << arr[i] << ' ';
cout << "\n";
}