#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) {
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;
while(adj[pl1] != -1) pl1 = adj[pl1];
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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1876 KB |
Output is correct |
2 |
Correct |
5 ms |
1524 KB |
Output is correct |
3 |
Correct |
8 ms |
1808 KB |
Output is correct |
4 |
Correct |
6 ms |
1560 KB |
Output is correct |
5 |
Correct |
8 ms |
1876 KB |
Output is correct |
6 |
Correct |
10 ms |
1824 KB |
Output is correct |
7 |
Correct |
8 ms |
1884 KB |
Output is correct |
8 |
Correct |
10 ms |
1852 KB |
Output is correct |
9 |
Correct |
14 ms |
1828 KB |
Output is correct |
10 |
Correct |
8 ms |
1876 KB |
Output is correct |
11 |
Correct |
20 ms |
3336 KB |
Output is correct |
12 |
Correct |
20 ms |
3408 KB |
Output is correct |
13 |
Correct |
25 ms |
3408 KB |
Output is correct |
14 |
Correct |
17 ms |
3396 KB |
Output is correct |
15 |
Correct |
11 ms |
2456 KB |
Output is correct |
16 |
Correct |
11 ms |
2428 KB |
Output is correct |
17 |
Correct |
12 ms |
2516 KB |
Output is correct |
18 |
Correct |
11 ms |
2492 KB |
Output is correct |
19 |
Correct |
12 ms |
2508 KB |
Output is correct |
20 |
Correct |
11 ms |
2436 KB |
Output is correct |
21 |
Correct |
11 ms |
2516 KB |
Output is correct |
22 |
Correct |
11 ms |
2480 KB |
Output is correct |
23 |
Correct |
11 ms |
2528 KB |
Output is correct |
24 |
Correct |
18 ms |
3404 KB |
Output is correct |
25 |
Correct |
24 ms |
3464 KB |
Output is correct |
26 |
Correct |
17 ms |
3404 KB |
Output is correct |
27 |
Correct |
17 ms |
3488 KB |
Output is correct |
28 |
Correct |
16 ms |
3404 KB |
Output is correct |
29 |
Correct |
17 ms |
3508 KB |
Output is correct |
30 |
Correct |
5 ms |
1236 KB |
Output is correct |
31 |
Correct |
8 ms |
1900 KB |
Output is correct |
32 |
Correct |
15 ms |
3156 KB |
Output is correct |
33 |
Correct |
17 ms |
3404 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
4 ms |
980 KB |
Output is correct |
36 |
Correct |
8 ms |
2004 KB |
Output is correct |
37 |
Correct |
15 ms |
3532 KB |
Output is correct |
38 |
Correct |
16 ms |
3432 KB |
Output is correct |
39 |
Correct |
16 ms |
3404 KB |
Output is correct |
40 |
Correct |
16 ms |
3652 KB |
Output is correct |
41 |
Correct |
19 ms |
3408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1876 KB |
Output is correct |
2 |
Correct |
5 ms |
1524 KB |
Output is correct |
3 |
Correct |
8 ms |
1808 KB |
Output is correct |
4 |
Correct |
6 ms |
1560 KB |
Output is correct |
5 |
Correct |
8 ms |
1876 KB |
Output is correct |
6 |
Correct |
10 ms |
1824 KB |
Output is correct |
7 |
Correct |
8 ms |
1884 KB |
Output is correct |
8 |
Correct |
10 ms |
1852 KB |
Output is correct |
9 |
Correct |
14 ms |
1828 KB |
Output is correct |
10 |
Correct |
8 ms |
1876 KB |
Output is correct |
11 |
Correct |
20 ms |
3336 KB |
Output is correct |
12 |
Correct |
20 ms |
3408 KB |
Output is correct |
13 |
Correct |
25 ms |
3408 KB |
Output is correct |
14 |
Correct |
17 ms |
3396 KB |
Output is correct |
15 |
Correct |
11 ms |
2456 KB |
Output is correct |
16 |
Correct |
11 ms |
2428 KB |
Output is correct |
17 |
Correct |
12 ms |
2516 KB |
Output is correct |
18 |
Correct |
11 ms |
2492 KB |
Output is correct |
19 |
Correct |
12 ms |
2508 KB |
Output is correct |
20 |
Correct |
11 ms |
2436 KB |
Output is correct |
21 |
Correct |
11 ms |
2516 KB |
Output is correct |
22 |
Correct |
11 ms |
2480 KB |
Output is correct |
23 |
Correct |
11 ms |
2528 KB |
Output is correct |
24 |
Correct |
18 ms |
3404 KB |
Output is correct |
25 |
Correct |
24 ms |
3464 KB |
Output is correct |
26 |
Correct |
17 ms |
3404 KB |
Output is correct |
27 |
Correct |
17 ms |
3488 KB |
Output is correct |
28 |
Correct |
16 ms |
3404 KB |
Output is correct |
29 |
Correct |
17 ms |
3508 KB |
Output is correct |
30 |
Correct |
5 ms |
1236 KB |
Output is correct |
31 |
Correct |
8 ms |
1900 KB |
Output is correct |
32 |
Correct |
15 ms |
3156 KB |
Output is correct |
33 |
Correct |
17 ms |
3404 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
4 ms |
980 KB |
Output is correct |
36 |
Correct |
8 ms |
2004 KB |
Output is correct |
37 |
Correct |
15 ms |
3532 KB |
Output is correct |
38 |
Correct |
16 ms |
3432 KB |
Output is correct |
39 |
Correct |
16 ms |
3404 KB |
Output is correct |
40 |
Correct |
16 ms |
3652 KB |
Output is correct |
41 |
Correct |
19 ms |
3408 KB |
Output is correct |
42 |
Incorrect |
8 ms |
1884 KB |
Output isn't correct |
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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |