답안 #484799

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
484799 2021-11-05T14:09:25 Z nhanbin03 Watermelon (INOI20_watermelon) C++14
49 / 100
94 ms 18584 KB
#include <bits/stdc++.h>
#define task "INOI20_watermelon"
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 1e5 + 10;

int n, k;
int a[N], inDeg[N], ans[N], rev[N];
vector<int> adj[N], pos[N];

bool buildDAG(int maxVal) {
    for (int i = 1; i <= n; i++) {
        pos[a[i]].push_back(i);
    }
    set<int> posSet;
    for (int i = maxVal; i >= 1; i--) {
        if (pos[i].empty()) return 0;
        for (int j = pos[i].size() - 1; j >= 0; j--)  {
            int x = pos[i][j];
            auto it = posSet.lower_bound(x);
            if (it != posSet.end()) {
                adj[x].push_back(*it);
                inDeg[*it]++;
            }
            if (it != posSet.begin()) {
                it--;
                if (j == 0 || pos[i][j - 1] < *it) {
                    adj[x].push_back(*it);
                    inDeg[*it]++;
                }
            }
            posSet.insert(x);
        }
    }
    return 1;
}

void solve() {
    priority_queue<int, vector<int>, greater<int>> pq;
    for (int i = 1; i <= n; i++) {
        if (inDeg[i] == 0) {
            pq.push(i);
        }
    }
    for (int i = 1; i <= n; i++) {
        int u = pq.top(); pq.pop();
        ans[u] = i;
        for (int v : adj[u]) {
            inDeg[v]--;
            if (inDeg[v] == 0) pq.push(v);
        }
    }
    for (int i = 1; i <= n; i++) {
        assert(inDeg[i] == 0);
        rev[ans[i]] = i;
    }
}

bool findNext() {
    set<int> s;
    for (int i = n; i >= 1; i--) {
        int u = rev[i];
//        cout << i << " " << u << endl;
        for (int v : adj[u]) {
            inDeg[v]++;
            s.erase(v);
        }
//        for (auto it = s.begin(); it != s.end(); it++) cout << *it << " "; cout << endl;
        auto it = s.lower_bound(u);
        if (it != s.end()) {
            priority_queue<int, vector<int>, greater<int>> pq;
            int u = *it;
            pq.push(u);
            for (int j = i; j <= n; j++) {
                int u = pq.top(); pq.pop();
                if (j == i) pq.push(rev[i]);
                ans[u] = j;
                for (int v : adj[u]) {
                    inDeg[v]--;
                    if (inDeg[v] == 0) pq.push(v);
                }
            }
            return 1;
        }
        s.insert(u);
    }
    return 0;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);
    if (fopen(task ".inp","r")) {
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    if (a[n] != -1) {
        cout << -1;
        return 0;
    }
    int maxVal = 0;
    for (int i = 1; i <= n; i++) {
        maxVal = max(maxVal, a[i]);
    }
    for (int i = n; i >= 1; i--) {
        if (a[i] == -1) a[i] = ++maxVal;
    }
//    for (int i = 1; i <= n; i++) {
//        cout << a[i] << " ";
//    }
//    cout << "\n";
    if (!buildDAG(maxVal)) {
        cout << -1;
        return 0;
    }
//    for (int i = 1; i <= n; i++) {
//        cout << i << ": ";
//        for (int j : adj[i]) {
//            cout << j << " ";
//        }
//        cout << "\n";
//    }
    solve();
    for (int i = 1; i < k; i++) {
        if (!findNext()) {
            cout << -1;
            return 0;
        }
        for (int j = 1; j <= n; j++) {
            assert(inDeg[j] == 0);
            rev[ans[j]] = j;
        }
    }
    for (int i = 1; i <= n; i++) {
        cout << ans[i] << " ";
    }
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |         freopen(task ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |         freopen(task ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5016 KB Output is correct
3 Correct 3 ms 4960 KB Output is correct
4 Incorrect 3 ms 4940 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5016 KB Output is correct
3 Correct 3 ms 4960 KB Output is correct
4 Incorrect 3 ms 4940 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 10360 KB Output is correct
2 Correct 10 ms 6308 KB Output is correct
3 Correct 38 ms 10404 KB Output is correct
4 Correct 10 ms 6220 KB Output is correct
5 Correct 29 ms 10364 KB Output is correct
6 Correct 35 ms 10392 KB Output is correct
7 Correct 30 ms 10384 KB Output is correct
8 Correct 30 ms 10368 KB Output is correct
9 Correct 30 ms 10460 KB Output is correct
10 Correct 30 ms 10400 KB Output is correct
11 Correct 75 ms 15776 KB Output is correct
12 Correct 60 ms 15644 KB Output is correct
13 Correct 64 ms 15616 KB Output is correct
14 Correct 58 ms 15724 KB Output is correct
15 Correct 16 ms 7520 KB Output is correct
16 Correct 20 ms 7608 KB Output is correct
17 Correct 27 ms 7484 KB Output is correct
18 Correct 16 ms 7524 KB Output is correct
19 Correct 19 ms 7544 KB Output is correct
20 Correct 20 ms 7584 KB Output is correct
21 Correct 18 ms 7624 KB Output is correct
22 Correct 16 ms 7628 KB Output is correct
23 Correct 17 ms 7640 KB Output is correct
24 Correct 64 ms 16600 KB Output is correct
25 Correct 62 ms 16580 KB Output is correct
26 Correct 65 ms 16816 KB Output is correct
27 Correct 82 ms 16296 KB Output is correct
28 Correct 76 ms 16460 KB Output is correct
29 Correct 62 ms 16316 KB Output is correct
30 Correct 18 ms 8168 KB Output is correct
31 Correct 36 ms 10412 KB Output is correct
32 Correct 82 ms 14820 KB Output is correct
33 Correct 81 ms 15796 KB Output is correct
34 Correct 9 ms 6100 KB Output is correct
35 Correct 16 ms 7204 KB Output is correct
36 Correct 38 ms 10408 KB Output is correct
37 Correct 86 ms 15820 KB Output is correct
38 Correct 72 ms 15972 KB Output is correct
39 Correct 84 ms 15892 KB Output is correct
40 Correct 59 ms 17164 KB Output is correct
41 Correct 64 ms 18584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 10360 KB Output is correct
2 Correct 10 ms 6308 KB Output is correct
3 Correct 38 ms 10404 KB Output is correct
4 Correct 10 ms 6220 KB Output is correct
5 Correct 29 ms 10364 KB Output is correct
6 Correct 35 ms 10392 KB Output is correct
7 Correct 30 ms 10384 KB Output is correct
8 Correct 30 ms 10368 KB Output is correct
9 Correct 30 ms 10460 KB Output is correct
10 Correct 30 ms 10400 KB Output is correct
11 Correct 75 ms 15776 KB Output is correct
12 Correct 60 ms 15644 KB Output is correct
13 Correct 64 ms 15616 KB Output is correct
14 Correct 58 ms 15724 KB Output is correct
15 Correct 16 ms 7520 KB Output is correct
16 Correct 20 ms 7608 KB Output is correct
17 Correct 27 ms 7484 KB Output is correct
18 Correct 16 ms 7524 KB Output is correct
19 Correct 19 ms 7544 KB Output is correct
20 Correct 20 ms 7584 KB Output is correct
21 Correct 18 ms 7624 KB Output is correct
22 Correct 16 ms 7628 KB Output is correct
23 Correct 17 ms 7640 KB Output is correct
24 Correct 64 ms 16600 KB Output is correct
25 Correct 62 ms 16580 KB Output is correct
26 Correct 65 ms 16816 KB Output is correct
27 Correct 82 ms 16296 KB Output is correct
28 Correct 76 ms 16460 KB Output is correct
29 Correct 62 ms 16316 KB Output is correct
30 Correct 18 ms 8168 KB Output is correct
31 Correct 36 ms 10412 KB Output is correct
32 Correct 82 ms 14820 KB Output is correct
33 Correct 81 ms 15796 KB Output is correct
34 Correct 9 ms 6100 KB Output is correct
35 Correct 16 ms 7204 KB Output is correct
36 Correct 38 ms 10408 KB Output is correct
37 Correct 86 ms 15820 KB Output is correct
38 Correct 72 ms 15972 KB Output is correct
39 Correct 84 ms 15892 KB Output is correct
40 Correct 59 ms 17164 KB Output is correct
41 Correct 64 ms 18584 KB Output is correct
42 Correct 29 ms 10376 KB Output is correct
43 Correct 9 ms 6312 KB Output is correct
44 Correct 30 ms 10456 KB Output is correct
45 Correct 10 ms 6320 KB Output is correct
46 Correct 10 ms 6312 KB Output is correct
47 Correct 10 ms 6304 KB Output is correct
48 Correct 11 ms 6220 KB Output is correct
49 Correct 37 ms 10472 KB Output is correct
50 Correct 30 ms 10368 KB Output is correct
51 Correct 29 ms 10360 KB Output is correct
52 Correct 61 ms 15644 KB Output is correct
53 Correct 57 ms 15636 KB Output is correct
54 Correct 18 ms 7628 KB Output is correct
55 Correct 21 ms 7568 KB Output is correct
56 Correct 69 ms 14728 KB Output is correct
57 Correct 79 ms 15864 KB Output is correct
58 Correct 94 ms 15916 KB Output is correct
59 Correct 64 ms 16064 KB Output is correct
60 Correct 79 ms 15984 KB Output is correct
61 Correct 64 ms 17228 KB Output is correct
62 Correct 59 ms 18032 KB Output is correct
63 Correct 68 ms 16452 KB Output is correct
64 Correct 60 ms 16868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5016 KB Output is correct
3 Correct 3 ms 4960 KB Output is correct
4 Incorrect 3 ms 4940 KB Output isn't correct
5 Halted 0 ms 0 KB -