Untitled
unknown
c_cpp
2 years ago
1.3 kB
3
Indexable
Never
#include <bits/stdc++.h> using namespace std; int max_el(vector<int> a) { int res = -1e9; for (int i = 0; i < a.size(); ++i) if (a[i] > res) res = a[i]; return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> permutation(n); for (int& elem : permutation) cin >> elem; for (int i = 0; i < n; ++i) if (permutation[i] > n) { cout << "-1\n"; return 0; } int max_elem = max_el(permutation); vector<int> a(max_elem, 0); for (int i = 0; i < n; ++i) { if (permutation[i] != 0) { if (a[permutation[i] - 1] == 0) a[permutation[i] - 1] = 1; else if (a[permutation[i] - 1] == 1) { cout << "-1\n"; return 0; } } } vector<int> b; for (int i = 0; i < a.size(); ++i) if (a[i] == 0) b.push_back(i + 1); for (int i = 0, j = 0; i < permutation.size(); ++i) if (permutation[i] == 0) { if (b.size() != 0) { permutation[i] = b.back(); b.pop_back(); } else permutation[i] = max_elem + (++j); } for (int elem : permutation) cout << elem << ' '; }