답안 #627055

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
627055 2022-08-12T06:00:45 Z AA_Surely Watermelon (INOI20_watermelon) C++14
31 / 100
21 ms 3468 KB
#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];
        assert(ans[pl] + 1 == ans[n - 1]);
        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 0 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 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 284 KB Output is correct
8 Correct 0 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 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Incorrect 1 ms 340 KB Output isn't correct
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 284 KB Output is correct
8 Correct 0 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 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Incorrect 1 ms 340 KB Output isn't correct
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 1748 KB Output is correct
2 Correct 6 ms 1236 KB Output is correct
3 Correct 10 ms 1748 KB Output is correct
4 Correct 5 ms 1236 KB Output is correct
5 Correct 9 ms 1820 KB Output is correct
6 Correct 9 ms 1756 KB Output is correct
7 Correct 10 ms 1748 KB Output is correct
8 Correct 8 ms 1748 KB Output is correct
9 Correct 10 ms 1748 KB Output is correct
10 Correct 8 ms 1748 KB Output is correct
11 Correct 20 ms 3216 KB Output is correct
12 Correct 16 ms 3188 KB Output is correct
13 Correct 16 ms 3156 KB Output is correct
14 Correct 16 ms 3172 KB Output is correct
15 Correct 11 ms 2260 KB Output is correct
16 Correct 11 ms 2256 KB Output is correct
17 Correct 16 ms 2160 KB Output is correct
18 Correct 11 ms 2192 KB Output is correct
19 Correct 10 ms 2260 KB Output is correct
20 Correct 10 ms 2260 KB Output is correct
21 Correct 10 ms 2260 KB Output is correct
22 Correct 11 ms 2260 KB Output is correct
23 Correct 10 ms 2216 KB Output is correct
24 Correct 16 ms 3260 KB Output is correct
25 Correct 16 ms 3208 KB Output is correct
26 Correct 18 ms 3320 KB Output is correct
27 Correct 17 ms 3156 KB Output is correct
28 Correct 21 ms 3192 KB Output is correct
29 Correct 16 ms 3168 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 2900 KB Output is correct
33 Correct 15 ms 3160 KB Output is correct
34 Correct 2 ms 596 KB Output is correct
35 Correct 3 ms 852 KB Output is correct
36 Correct 8 ms 1820 KB Output is correct
37 Correct 15 ms 3220 KB Output is correct
38 Correct 15 ms 3272 KB Output is correct
39 Correct 18 ms 3284 KB Output is correct
40 Correct 17 ms 3468 KB Output is correct
41 Correct 17 ms 3216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 1748 KB Output is correct
2 Correct 6 ms 1236 KB Output is correct
3 Correct 10 ms 1748 KB Output is correct
4 Correct 5 ms 1236 KB Output is correct
5 Correct 9 ms 1820 KB Output is correct
6 Correct 9 ms 1756 KB Output is correct
7 Correct 10 ms 1748 KB Output is correct
8 Correct 8 ms 1748 KB Output is correct
9 Correct 10 ms 1748 KB Output is correct
10 Correct 8 ms 1748 KB Output is correct
11 Correct 20 ms 3216 KB Output is correct
12 Correct 16 ms 3188 KB Output is correct
13 Correct 16 ms 3156 KB Output is correct
14 Correct 16 ms 3172 KB Output is correct
15 Correct 11 ms 2260 KB Output is correct
16 Correct 11 ms 2256 KB Output is correct
17 Correct 16 ms 2160 KB Output is correct
18 Correct 11 ms 2192 KB Output is correct
19 Correct 10 ms 2260 KB Output is correct
20 Correct 10 ms 2260 KB Output is correct
21 Correct 10 ms 2260 KB Output is correct
22 Correct 11 ms 2260 KB Output is correct
23 Correct 10 ms 2216 KB Output is correct
24 Correct 16 ms 3260 KB Output is correct
25 Correct 16 ms 3208 KB Output is correct
26 Correct 18 ms 3320 KB Output is correct
27 Correct 17 ms 3156 KB Output is correct
28 Correct 21 ms 3192 KB Output is correct
29 Correct 16 ms 3168 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 2900 KB Output is correct
33 Correct 15 ms 3160 KB Output is correct
34 Correct 2 ms 596 KB Output is correct
35 Correct 3 ms 852 KB Output is correct
36 Correct 8 ms 1820 KB Output is correct
37 Correct 15 ms 3220 KB Output is correct
38 Correct 15 ms 3272 KB Output is correct
39 Correct 18 ms 3284 KB Output is correct
40 Correct 17 ms 3468 KB Output is correct
41 Correct 17 ms 3216 KB Output is correct
42 Incorrect 8 ms 1748 KB Output isn't correct
43 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 284 KB Output is correct
8 Correct 0 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 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Incorrect 1 ms 340 KB Output isn't correct
15 Halted 0 ms 0 KB -