답안 #473938

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
473938 2021-09-16T12:35:23 Z model_code Tortoise (CEOI21_tortoise) C++17
100 / 100
1096 ms 46924 KB
#include <bits/stdc++.h>
using namespace std;

#define _ << " " <<

const int MAXN = 5e5 + 5;
const int off = 1 << 19;
const int inf = 1e9;

int a[MAXN];
int l[MAXN];
int r[MAXN];
int before[MAXN];

unordered_map<int, int> specialChair;

typedef pair<int, int> pii;

struct Tournament {
    vector<int> t;
    vector<int> p;

    Tournament() {
        t.resize(2 * off);
        p.resize(2 * off);
    }

    void init() {
        for (int i = off; i < 2 * off; ++i) {
            t[i] = inf + (i - off);
        }
        for (int i = off - 1; i; i--) {
            t[i] = min(t[i * 2], t[i * 2 + 1]);
        }
    }

    void prop(int x) {
        if (p[x]) {
            t[x] += p[x];
            if (x < off) {
                p[x * 2] += p[x];
                p[x * 2 + 1] += p[x];
            }
            p[x] = 0;
        }
    }

    void upd(int a, int b, int v, int x = 1, int lo = 0, int hi = off) {
        if (lo >= a && hi <= b) {
            p[x] += v;
            prop(x);
            return;
        }
        prop(x);
        if (lo >= b || hi <= a) return;
        int mi = (lo + hi) >> 1;
        upd(a, b, v, x * 2, lo, mi);
        upd(a, b, v, x * 2 + 1, mi, hi);
        t[x] = min(t[x * 2], t[x * 2 + 1]);
    }
    int get(int a, int b, int x = 1, int lo = 0, int hi = off) {
        if (lo >= b || hi <= a) return inf;
        prop(x);
        if (lo >= a && hi <= b) {
            return t[x];
        }

        int mi = (lo + hi) >> 1;
        return min(get(a, b, x * 2, lo, mi),
                   get(a, b, x * 2 + 1, mi, hi));
    }
} special, updates, tracker;

void moveSpecial(int a, int b) {
    special.upd(a, a+1, +inf);
    special.upd(b, b+1, -inf);
}
void addSpecial(int a) {
    special.upd(a, a+1, -inf);
}

long long sum;

void solve(int x, int d, int type) {
    int cnt = 0;
    while(a[x] > 0) {

        // check if this is valid with previous chairs
        if (tracker.get(x, off) < 2 * d) {
            break;
        }

        // check if this chair can be taken
        if (x - updates.get(x, x + 1) < type * d * 2) {
            break;
        }

        // if this is segment with right storage
        if (r[x] != -1) {
            int spec = specialChair[r[x]];
            if (type) spec = min(spec, x);

            if (a[spec] == 1 && spec == x) {
                if (!type) break;
                spec = before[spec];
            }

            if (spec <= l[x]) break;
            if (a[spec] <= 0) break;

            moveSpecial(specialChair[r[x]], spec);
            special.upd(x + type, off, -2 * d);

            int diff = special.get(0, off);

            if (diff < 0) {
                special.upd(x + type, off, 2 * d);
                moveSpecial(spec, specialChair[r[x]]);
                break;
            }
            specialChair[r[x]] = spec;
        }

        if (cnt == 0) {
            tracker.upd(x, x + 1, - inf - 2 * d * (type - 1));
        }

        tracker.upd(x, off, -2 * d);
        updates.upd(x, off, 2 * d);

        a[x] --;
        sum --;
        cnt ++;
    }
}

int main() {
    int n; cin >> n;

    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        if (a[i] != -1) sum += a[i];
    }

    int tmp = -1;
    for (int i = 0; i < n; ++i) {
        if (a[i] == -1) {
            tmp = i;
        }
        l[i] = tmp;
    }
    tmp = -1;
    for (int i = n - 1; i >= 0; --i) {
        if (a[i] == -1) {
            tmp = i;
        }
        r[i] = tmp;
    }

    tmp = -1;
    for (int i = 0; i < n; ++i) {
        before[i] = tmp;
        if (a[i] > 0) {
            tmp = i;
        }
    }

    special.init();
    tracker.init();

    // setup sepcial chair moves (left on first pass)
    for (int i = 0; i < n; ++i) {
        if (a[i] > 0 && r[i] != -1 && before[r[i]] == i) {
            addSpecial(i);
            specialChair[r[i]] = i;
        }
    }

    vector<pair<int, pii>> candidates;
    for (int i = 0; i < n; ++i) {
        if (a[i] > 0) {
            if (r[i] != -1) {
                candidates.push_back({r[i] - i, {i, 1}});
            }
            if (l[i] != -1) {
                candidates.push_back({i - l[i], {i, 0}});
            }
        }
    }
    sort(candidates.begin(), candidates.end());

    for (auto candidate : candidates) {
        int x = candidate.second.first;
        int d = candidate.first;
        int type = candidate.second.second;

        solve(x, d, type);
    }

    for (int i = 0; i < n; ++i) {
        if (a[i] > 0 && r[i] != -1) {
            a[r[i]] = -2;
        }
        if (a[i] == -2) sum --;
    }

    cout << sum << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 24908 KB Output is correct
2 Correct 13 ms 24908 KB Output is correct
3 Correct 14 ms 24908 KB Output is correct
4 Correct 13 ms 24968 KB Output is correct
5 Correct 13 ms 24908 KB Output is correct
6 Correct 14 ms 24916 KB Output is correct
7 Correct 28 ms 24932 KB Output is correct
8 Correct 13 ms 24908 KB Output is correct
9 Correct 13 ms 24908 KB Output is correct
10 Correct 13 ms 24980 KB Output is correct
11 Correct 13 ms 24976 KB Output is correct
12 Correct 13 ms 24976 KB Output is correct
13 Correct 13 ms 24908 KB Output is correct
14 Correct 14 ms 24952 KB Output is correct
15 Correct 14 ms 24924 KB Output is correct
16 Correct 14 ms 24964 KB Output is correct
17 Correct 13 ms 24860 KB Output is correct
18 Correct 14 ms 24908 KB Output is correct
19 Correct 13 ms 24896 KB Output is correct
20 Correct 15 ms 24972 KB Output is correct
21 Correct 14 ms 24980 KB Output is correct
22 Correct 13 ms 24900 KB Output is correct
23 Correct 13 ms 24908 KB Output is correct
24 Correct 13 ms 24880 KB Output is correct
25 Correct 13 ms 24908 KB Output is correct
26 Correct 16 ms 24976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 24908 KB Output is correct
2 Correct 13 ms 24908 KB Output is correct
3 Correct 14 ms 24908 KB Output is correct
4 Correct 13 ms 24968 KB Output is correct
5 Correct 13 ms 24908 KB Output is correct
6 Correct 14 ms 24916 KB Output is correct
7 Correct 28 ms 24932 KB Output is correct
8 Correct 13 ms 24908 KB Output is correct
9 Correct 13 ms 24908 KB Output is correct
10 Correct 13 ms 24980 KB Output is correct
11 Correct 13 ms 24976 KB Output is correct
12 Correct 13 ms 24976 KB Output is correct
13 Correct 13 ms 24908 KB Output is correct
14 Correct 14 ms 24952 KB Output is correct
15 Correct 14 ms 24924 KB Output is correct
16 Correct 14 ms 24964 KB Output is correct
17 Correct 13 ms 24860 KB Output is correct
18 Correct 14 ms 24908 KB Output is correct
19 Correct 13 ms 24896 KB Output is correct
20 Correct 15 ms 24972 KB Output is correct
21 Correct 14 ms 24980 KB Output is correct
22 Correct 13 ms 24900 KB Output is correct
23 Correct 13 ms 24908 KB Output is correct
24 Correct 13 ms 24880 KB Output is correct
25 Correct 13 ms 24908 KB Output is correct
26 Correct 16 ms 24976 KB Output is correct
27 Correct 13 ms 24960 KB Output is correct
28 Correct 14 ms 24944 KB Output is correct
29 Correct 14 ms 24928 KB Output is correct
30 Correct 14 ms 24920 KB Output is correct
31 Correct 14 ms 24888 KB Output is correct
32 Correct 14 ms 24908 KB Output is correct
33 Correct 15 ms 24908 KB Output is correct
34 Correct 17 ms 24908 KB Output is correct
35 Correct 17 ms 24908 KB Output is correct
36 Correct 14 ms 24908 KB Output is correct
37 Correct 14 ms 24908 KB Output is correct
38 Correct 13 ms 24956 KB Output is correct
39 Correct 13 ms 24908 KB Output is correct
40 Correct 14 ms 24984 KB Output is correct
41 Correct 14 ms 24948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 24908 KB Output is correct
2 Correct 13 ms 24908 KB Output is correct
3 Correct 14 ms 24908 KB Output is correct
4 Correct 13 ms 24968 KB Output is correct
5 Correct 13 ms 24908 KB Output is correct
6 Correct 14 ms 24916 KB Output is correct
7 Correct 28 ms 24932 KB Output is correct
8 Correct 13 ms 24908 KB Output is correct
9 Correct 13 ms 24908 KB Output is correct
10 Correct 13 ms 24980 KB Output is correct
11 Correct 13 ms 24976 KB Output is correct
12 Correct 13 ms 24976 KB Output is correct
13 Correct 13 ms 24908 KB Output is correct
14 Correct 14 ms 24952 KB Output is correct
15 Correct 14 ms 24924 KB Output is correct
16 Correct 14 ms 24964 KB Output is correct
17 Correct 13 ms 24860 KB Output is correct
18 Correct 14 ms 24908 KB Output is correct
19 Correct 13 ms 24896 KB Output is correct
20 Correct 15 ms 24972 KB Output is correct
21 Correct 14 ms 24980 KB Output is correct
22 Correct 13 ms 24900 KB Output is correct
23 Correct 13 ms 24908 KB Output is correct
24 Correct 13 ms 24880 KB Output is correct
25 Correct 13 ms 24908 KB Output is correct
26 Correct 16 ms 24976 KB Output is correct
27 Correct 13 ms 24960 KB Output is correct
28 Correct 14 ms 24944 KB Output is correct
29 Correct 14 ms 24928 KB Output is correct
30 Correct 14 ms 24920 KB Output is correct
31 Correct 14 ms 24888 KB Output is correct
32 Correct 14 ms 24908 KB Output is correct
33 Correct 15 ms 24908 KB Output is correct
34 Correct 17 ms 24908 KB Output is correct
35 Correct 17 ms 24908 KB Output is correct
36 Correct 14 ms 24908 KB Output is correct
37 Correct 14 ms 24908 KB Output is correct
38 Correct 13 ms 24956 KB Output is correct
39 Correct 13 ms 24908 KB Output is correct
40 Correct 14 ms 24984 KB Output is correct
41 Correct 14 ms 24948 KB Output is correct
42 Correct 13 ms 24908 KB Output is correct
43 Correct 14 ms 24908 KB Output is correct
44 Correct 14 ms 24868 KB Output is correct
45 Correct 14 ms 24992 KB Output is correct
46 Correct 13 ms 24908 KB Output is correct
47 Correct 15 ms 24980 KB Output is correct
48 Correct 14 ms 24904 KB Output is correct
49 Correct 14 ms 24908 KB Output is correct
50 Correct 17 ms 24916 KB Output is correct
51 Correct 14 ms 24936 KB Output is correct
52 Correct 14 ms 24888 KB Output is correct
53 Correct 14 ms 24908 KB Output is correct
54 Correct 15 ms 24940 KB Output is correct
55 Correct 14 ms 24968 KB Output is correct
56 Correct 14 ms 24936 KB Output is correct
57 Correct 14 ms 24980 KB Output is correct
58 Correct 15 ms 24908 KB Output is correct
59 Correct 16 ms 24908 KB Output is correct
60 Correct 16 ms 24908 KB Output is correct
61 Correct 14 ms 24992 KB Output is correct
62 Correct 14 ms 24916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 24908 KB Output is correct
2 Correct 13 ms 24908 KB Output is correct
3 Correct 14 ms 24908 KB Output is correct
4 Correct 13 ms 24968 KB Output is correct
5 Correct 13 ms 24908 KB Output is correct
6 Correct 14 ms 24916 KB Output is correct
7 Correct 28 ms 24932 KB Output is correct
8 Correct 13 ms 24908 KB Output is correct
9 Correct 13 ms 24908 KB Output is correct
10 Correct 13 ms 24980 KB Output is correct
11 Correct 13 ms 24976 KB Output is correct
12 Correct 13 ms 24976 KB Output is correct
13 Correct 13 ms 24908 KB Output is correct
14 Correct 14 ms 24952 KB Output is correct
15 Correct 14 ms 24924 KB Output is correct
16 Correct 14 ms 24964 KB Output is correct
17 Correct 13 ms 24860 KB Output is correct
18 Correct 14 ms 24908 KB Output is correct
19 Correct 13 ms 24896 KB Output is correct
20 Correct 15 ms 24972 KB Output is correct
21 Correct 14 ms 24980 KB Output is correct
22 Correct 13 ms 24900 KB Output is correct
23 Correct 13 ms 24908 KB Output is correct
24 Correct 13 ms 24880 KB Output is correct
25 Correct 13 ms 24908 KB Output is correct
26 Correct 16 ms 24976 KB Output is correct
27 Correct 13 ms 24960 KB Output is correct
28 Correct 14 ms 24944 KB Output is correct
29 Correct 14 ms 24928 KB Output is correct
30 Correct 14 ms 24920 KB Output is correct
31 Correct 14 ms 24888 KB Output is correct
32 Correct 14 ms 24908 KB Output is correct
33 Correct 15 ms 24908 KB Output is correct
34 Correct 17 ms 24908 KB Output is correct
35 Correct 17 ms 24908 KB Output is correct
36 Correct 14 ms 24908 KB Output is correct
37 Correct 14 ms 24908 KB Output is correct
38 Correct 13 ms 24956 KB Output is correct
39 Correct 13 ms 24908 KB Output is correct
40 Correct 14 ms 24984 KB Output is correct
41 Correct 14 ms 24948 KB Output is correct
42 Correct 13 ms 24908 KB Output is correct
43 Correct 14 ms 24908 KB Output is correct
44 Correct 14 ms 24868 KB Output is correct
45 Correct 14 ms 24992 KB Output is correct
46 Correct 13 ms 24908 KB Output is correct
47 Correct 15 ms 24980 KB Output is correct
48 Correct 14 ms 24904 KB Output is correct
49 Correct 14 ms 24908 KB Output is correct
50 Correct 17 ms 24916 KB Output is correct
51 Correct 14 ms 24936 KB Output is correct
52 Correct 14 ms 24888 KB Output is correct
53 Correct 14 ms 24908 KB Output is correct
54 Correct 15 ms 24940 KB Output is correct
55 Correct 14 ms 24968 KB Output is correct
56 Correct 14 ms 24936 KB Output is correct
57 Correct 14 ms 24980 KB Output is correct
58 Correct 15 ms 24908 KB Output is correct
59 Correct 16 ms 24908 KB Output is correct
60 Correct 16 ms 24908 KB Output is correct
61 Correct 14 ms 24992 KB Output is correct
62 Correct 14 ms 24916 KB Output is correct
63 Correct 19 ms 25064 KB Output is correct
64 Correct 22 ms 25164 KB Output is correct
65 Correct 20 ms 25056 KB Output is correct
66 Correct 21 ms 25096 KB Output is correct
67 Correct 18 ms 25220 KB Output is correct
68 Correct 20 ms 25164 KB Output is correct
69 Correct 22 ms 25164 KB Output is correct
70 Correct 21 ms 25184 KB Output is correct
71 Correct 26 ms 25276 KB Output is correct
72 Correct 28 ms 25328 KB Output is correct
73 Correct 23 ms 25292 KB Output is correct
74 Correct 23 ms 25292 KB Output is correct
75 Correct 14 ms 24952 KB Output is correct
76 Correct 21 ms 25184 KB Output is correct
77 Correct 20 ms 25164 KB Output is correct
78 Correct 20 ms 25164 KB Output is correct
79 Correct 20 ms 25236 KB Output is correct
80 Correct 20 ms 25228 KB Output is correct
81 Correct 20 ms 25192 KB Output is correct
82 Correct 20 ms 25164 KB Output is correct
83 Correct 27 ms 25180 KB Output is correct
84 Correct 22 ms 25228 KB Output is correct
85 Correct 23 ms 25108 KB Output is correct
86 Correct 23 ms 25164 KB Output is correct
87 Correct 23 ms 25216 KB Output is correct
88 Correct 22 ms 25224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 24908 KB Output is correct
2 Correct 13 ms 24908 KB Output is correct
3 Correct 14 ms 24908 KB Output is correct
4 Correct 13 ms 24968 KB Output is correct
5 Correct 13 ms 24908 KB Output is correct
6 Correct 14 ms 24916 KB Output is correct
7 Correct 28 ms 24932 KB Output is correct
8 Correct 13 ms 24908 KB Output is correct
9 Correct 13 ms 24908 KB Output is correct
10 Correct 13 ms 24980 KB Output is correct
11 Correct 13 ms 24976 KB Output is correct
12 Correct 13 ms 24976 KB Output is correct
13 Correct 13 ms 24908 KB Output is correct
14 Correct 14 ms 24952 KB Output is correct
15 Correct 14 ms 24924 KB Output is correct
16 Correct 14 ms 24964 KB Output is correct
17 Correct 13 ms 24860 KB Output is correct
18 Correct 14 ms 24908 KB Output is correct
19 Correct 13 ms 24896 KB Output is correct
20 Correct 15 ms 24972 KB Output is correct
21 Correct 14 ms 24980 KB Output is correct
22 Correct 13 ms 24900 KB Output is correct
23 Correct 13 ms 24908 KB Output is correct
24 Correct 13 ms 24880 KB Output is correct
25 Correct 13 ms 24908 KB Output is correct
26 Correct 16 ms 24976 KB Output is correct
27 Correct 13 ms 24960 KB Output is correct
28 Correct 14 ms 24944 KB Output is correct
29 Correct 14 ms 24928 KB Output is correct
30 Correct 14 ms 24920 KB Output is correct
31 Correct 14 ms 24888 KB Output is correct
32 Correct 14 ms 24908 KB Output is correct
33 Correct 15 ms 24908 KB Output is correct
34 Correct 17 ms 24908 KB Output is correct
35 Correct 17 ms 24908 KB Output is correct
36 Correct 14 ms 24908 KB Output is correct
37 Correct 14 ms 24908 KB Output is correct
38 Correct 13 ms 24956 KB Output is correct
39 Correct 13 ms 24908 KB Output is correct
40 Correct 14 ms 24984 KB Output is correct
41 Correct 14 ms 24948 KB Output is correct
42 Correct 13 ms 24908 KB Output is correct
43 Correct 14 ms 24908 KB Output is correct
44 Correct 14 ms 24868 KB Output is correct
45 Correct 14 ms 24992 KB Output is correct
46 Correct 13 ms 24908 KB Output is correct
47 Correct 15 ms 24980 KB Output is correct
48 Correct 14 ms 24904 KB Output is correct
49 Correct 14 ms 24908 KB Output is correct
50 Correct 17 ms 24916 KB Output is correct
51 Correct 14 ms 24936 KB Output is correct
52 Correct 14 ms 24888 KB Output is correct
53 Correct 14 ms 24908 KB Output is correct
54 Correct 15 ms 24940 KB Output is correct
55 Correct 14 ms 24968 KB Output is correct
56 Correct 14 ms 24936 KB Output is correct
57 Correct 14 ms 24980 KB Output is correct
58 Correct 15 ms 24908 KB Output is correct
59 Correct 16 ms 24908 KB Output is correct
60 Correct 16 ms 24908 KB Output is correct
61 Correct 14 ms 24992 KB Output is correct
62 Correct 14 ms 24916 KB Output is correct
63 Correct 19 ms 25064 KB Output is correct
64 Correct 22 ms 25164 KB Output is correct
65 Correct 20 ms 25056 KB Output is correct
66 Correct 21 ms 25096 KB Output is correct
67 Correct 18 ms 25220 KB Output is correct
68 Correct 20 ms 25164 KB Output is correct
69 Correct 22 ms 25164 KB Output is correct
70 Correct 21 ms 25184 KB Output is correct
71 Correct 26 ms 25276 KB Output is correct
72 Correct 28 ms 25328 KB Output is correct
73 Correct 23 ms 25292 KB Output is correct
74 Correct 23 ms 25292 KB Output is correct
75 Correct 14 ms 24952 KB Output is correct
76 Correct 21 ms 25184 KB Output is correct
77 Correct 20 ms 25164 KB Output is correct
78 Correct 20 ms 25164 KB Output is correct
79 Correct 20 ms 25236 KB Output is correct
80 Correct 20 ms 25228 KB Output is correct
81 Correct 20 ms 25192 KB Output is correct
82 Correct 20 ms 25164 KB Output is correct
83 Correct 27 ms 25180 KB Output is correct
84 Correct 22 ms 25228 KB Output is correct
85 Correct 23 ms 25108 KB Output is correct
86 Correct 23 ms 25164 KB Output is correct
87 Correct 23 ms 25216 KB Output is correct
88 Correct 22 ms 25224 KB Output is correct
89 Correct 504 ms 38308 KB Output is correct
90 Correct 498 ms 38332 KB Output is correct
91 Correct 546 ms 38348 KB Output is correct
92 Correct 730 ms 42104 KB Output is correct
93 Correct 737 ms 41984 KB Output is correct
94 Correct 747 ms 41928 KB Output is correct
95 Correct 566 ms 38480 KB Output is correct
96 Correct 622 ms 38604 KB Output is correct
97 Correct 672 ms 40656 KB Output is correct
98 Correct 519 ms 39140 KB Output is correct
99 Correct 1080 ms 46360 KB Output is correct
100 Correct 1096 ms 46924 KB Output is correct
101 Correct 1068 ms 46324 KB Output is correct
102 Correct 1048 ms 45780 KB Output is correct
103 Correct 107 ms 32700 KB Output is correct
104 Correct 60 ms 26292 KB Output is correct
105 Correct 61 ms 26312 KB Output is correct
106 Correct 57 ms 26312 KB Output is correct
107 Correct 46 ms 25988 KB Output is correct
108 Correct 54 ms 26308 KB Output is correct
109 Correct 470 ms 32756 KB Output is correct