#include <bits/stdc++.h>
#define FOR(i, x, n) for(int i = x; i < n; i++)
#define F0R(i, n) FOR(i, 0, n)
#define ROF(i, x, n) for(int i = n - 1; i >= x; i--)
#define R0F(i, n) ROF(i, 0, n)
#define WTF cout << "WTF" << endl
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define F first
#define S second
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
const int N = 1e5 + 7;
const int LOG = 22;
const int INF = 1e9 + 7;
int n, k;
int ns[N], ans[N], adj[N];
int lst[N], pl[N], big[N];
PII st[N];
int change() {
int bc = 0, oc = 0;
F0R(i, n) {
if (ns[i] == n + 1) bc++;
if (ns[i] == 1) oc++;
}
if (!oc || (oc == 1 && bc == 1)) return cout << -1, 0;
if (bc > 1) {
assert(0);
int pl = -1;
F0R(i, n) if (ns[i] == 1) pl = i;
while(adj[pl] != -1) pl = adj[pl];
swap(ans[pl], ans[n - 1]);
}
else {
int pl1 = -1;
F0R(i, n) if (ns[i] == 1) pl1 = i;
int pl2 = -1;
F0R(i, pl1) if (ns[i] == 1) pl2 = i;
while(adj[pl2] != -1) pl2 = adj[pl2];
swap(ans[pl1], ans[pl2]);
}
F0R(i, n) cout << ans[i] << ' ';
return 0;
}
int main() {
IOS;
cin >> n >> k;
F0R(i, n) cin >> ns[i];
if (ns[n - 1] != -1) return cout << -1, 0;
F0R(i, n) if (ns[i] == -1) ns[i] = n + 1;
fill(pl, pl + n + 2, n + 1);
fill(adj, adj + n, -1);
R0F(i, n) {
pl[ ns[i] ] = i;
if (ns[i] == n + 1) continue;
if (ns[i] == 1) lst[i] = i;
else {
lst[i] = pl[ ns[i] - 1 ];
adj[ pl[ ns[i] - 1 ] ] = i;
}
}
stack<int> keep;
R0F(i, n) {
while(!keep.empty() && ns[keep.top()] < ns[i]) keep.pop();
big[i] = n + 1;
if (!keep.empty()) big[i] = keep.top();
keep.push(i);
}
F0R(i, n) if (ns[i] != 1 && ns[i] != n + 1)
if (lst[i] > big[i]) return cout << -1, 0;
int v = 1;
F0R(i, n) if (ns[i] == 1) {
int now = i;
while(now != -1) {
ans[now] = v++;
now = adj[now];
}
}
R0F(i, n) if (ns[i] == n + 1) ans[i] = v++;
if (k == 2) change();
else F0R(i, n) cout << ans[i] << ' ';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1748 KB |
Output is correct |
2 |
Correct |
5 ms |
1244 KB |
Output is correct |
3 |
Correct |
10 ms |
1748 KB |
Output is correct |
4 |
Correct |
5 ms |
1224 KB |
Output is correct |
5 |
Correct |
11 ms |
1824 KB |
Output is correct |
6 |
Correct |
9 ms |
1784 KB |
Output is correct |
7 |
Correct |
8 ms |
1748 KB |
Output is correct |
8 |
Correct |
9 ms |
1748 KB |
Output is correct |
9 |
Correct |
8 ms |
1748 KB |
Output is correct |
10 |
Correct |
8 ms |
1748 KB |
Output is correct |
11 |
Correct |
19 ms |
3180 KB |
Output is correct |
12 |
Correct |
15 ms |
3228 KB |
Output is correct |
13 |
Correct |
16 ms |
3192 KB |
Output is correct |
14 |
Correct |
16 ms |
3156 KB |
Output is correct |
15 |
Correct |
10 ms |
2200 KB |
Output is correct |
16 |
Correct |
10 ms |
2164 KB |
Output is correct |
17 |
Correct |
10 ms |
2260 KB |
Output is correct |
18 |
Correct |
9 ms |
2260 KB |
Output is correct |
19 |
Correct |
10 ms |
2260 KB |
Output is correct |
20 |
Correct |
10 ms |
2260 KB |
Output is correct |
21 |
Correct |
11 ms |
2280 KB |
Output is correct |
22 |
Correct |
10 ms |
2292 KB |
Output is correct |
23 |
Correct |
9 ms |
2168 KB |
Output is correct |
24 |
Correct |
16 ms |
3200 KB |
Output is correct |
25 |
Correct |
18 ms |
3152 KB |
Output is correct |
26 |
Correct |
15 ms |
3160 KB |
Output is correct |
27 |
Correct |
16 ms |
3232 KB |
Output is correct |
28 |
Correct |
16 ms |
3156 KB |
Output is correct |
29 |
Correct |
17 ms |
3368 KB |
Output is correct |
30 |
Correct |
5 ms |
1108 KB |
Output is correct |
31 |
Correct |
8 ms |
1748 KB |
Output is correct |
32 |
Correct |
14 ms |
2956 KB |
Output is correct |
33 |
Correct |
16 ms |
3272 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
4 ms |
852 KB |
Output is correct |
36 |
Correct |
11 ms |
1748 KB |
Output is correct |
37 |
Correct |
15 ms |
3176 KB |
Output is correct |
38 |
Correct |
17 ms |
3228 KB |
Output is correct |
39 |
Correct |
17 ms |
3276 KB |
Output is correct |
40 |
Correct |
16 ms |
3412 KB |
Output is correct |
41 |
Correct |
24 ms |
3148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1748 KB |
Output is correct |
2 |
Correct |
5 ms |
1244 KB |
Output is correct |
3 |
Correct |
10 ms |
1748 KB |
Output is correct |
4 |
Correct |
5 ms |
1224 KB |
Output is correct |
5 |
Correct |
11 ms |
1824 KB |
Output is correct |
6 |
Correct |
9 ms |
1784 KB |
Output is correct |
7 |
Correct |
8 ms |
1748 KB |
Output is correct |
8 |
Correct |
9 ms |
1748 KB |
Output is correct |
9 |
Correct |
8 ms |
1748 KB |
Output is correct |
10 |
Correct |
8 ms |
1748 KB |
Output is correct |
11 |
Correct |
19 ms |
3180 KB |
Output is correct |
12 |
Correct |
15 ms |
3228 KB |
Output is correct |
13 |
Correct |
16 ms |
3192 KB |
Output is correct |
14 |
Correct |
16 ms |
3156 KB |
Output is correct |
15 |
Correct |
10 ms |
2200 KB |
Output is correct |
16 |
Correct |
10 ms |
2164 KB |
Output is correct |
17 |
Correct |
10 ms |
2260 KB |
Output is correct |
18 |
Correct |
9 ms |
2260 KB |
Output is correct |
19 |
Correct |
10 ms |
2260 KB |
Output is correct |
20 |
Correct |
10 ms |
2260 KB |
Output is correct |
21 |
Correct |
11 ms |
2280 KB |
Output is correct |
22 |
Correct |
10 ms |
2292 KB |
Output is correct |
23 |
Correct |
9 ms |
2168 KB |
Output is correct |
24 |
Correct |
16 ms |
3200 KB |
Output is correct |
25 |
Correct |
18 ms |
3152 KB |
Output is correct |
26 |
Correct |
15 ms |
3160 KB |
Output is correct |
27 |
Correct |
16 ms |
3232 KB |
Output is correct |
28 |
Correct |
16 ms |
3156 KB |
Output is correct |
29 |
Correct |
17 ms |
3368 KB |
Output is correct |
30 |
Correct |
5 ms |
1108 KB |
Output is correct |
31 |
Correct |
8 ms |
1748 KB |
Output is correct |
32 |
Correct |
14 ms |
2956 KB |
Output is correct |
33 |
Correct |
16 ms |
3272 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
4 ms |
852 KB |
Output is correct |
36 |
Correct |
11 ms |
1748 KB |
Output is correct |
37 |
Correct |
15 ms |
3176 KB |
Output is correct |
38 |
Correct |
17 ms |
3228 KB |
Output is correct |
39 |
Correct |
17 ms |
3276 KB |
Output is correct |
40 |
Correct |
16 ms |
3412 KB |
Output is correct |
41 |
Correct |
24 ms |
3148 KB |
Output is correct |
42 |
Runtime error |
7 ms |
2900 KB |
Execution killed with signal 6 |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
13 |
Halted |
0 ms |
0 KB |
- |