Submission #53739

# Submission time Handle Problem Language Result Execution time Memory
53739 2018-07-01T06:14:42 Z aome Roller Coaster Railroad (IOI16_railroad) C++17
100 / 100
373 ms 89932 KB
#include "railroad.h"

#include <bits/stdc++.h>

using namespace std;

const int N = 200005;
const int INF = 1e9;

int par[N * 2], cnt[N * 2];

int find(int u) { return u == par[u] ? u : par[u] = find(par[u]); }

bool join(int u, int v) { u = find(u), v = find(v), par[u] = v; return u != v; }

struct Edge {
    int w, u, v;

    bool operator < (const Edge& rhs) const {
        return w < rhs.w;
    }
};

long long plan_roller_coaster(vector<int> s, vector<int> t) {
    s.push_back(INF), t.push_back(1);
    vector<int> vec;
    for (auto i : s) vec.push_back(i);
    for (auto i : t) vec.push_back(i);
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    int n = s.size(), m = vec.size();
    for (int i = 0; i < m; ++i) par[i] = i;
    for (int i = 0; i < n; ++i) {
        s[i] = lower_bound(vec.begin(), vec.end(), s[i]) - vec.begin();
        t[i] = lower_bound(vec.begin(), vec.end(), t[i]) - vec.begin();
        if (s[i] < t[i]) cnt[s[i]]++, cnt[t[i]]--;
        else cnt[t[i]]--, cnt[s[i]]++;
        join(s[i], t[i]); 
    }
    for (int i = 1; i < m; ++i) cnt[i] += cnt[i - 1];
    long long res = 0;
    for (int i = 0; i < m; ++i) {
        if (cnt[i] < 0) join(i, i + 1);
        if (cnt[i] > 0) {
            join(i, i + 1), res += 1LL * cnt[i] * (vec[i + 1] - vec[i]);
        }
    }
    vector<Edge> edge;
    for (int i = 1; i < m; ++i) edge.push_back({vec[i] - vec[i - 1], i, i - 1});
    sort(edge.begin(), edge.end());
    for (auto i : edge) {
        if (join(i.u, i.v)) res += i.w;
    }
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB n = 2
2 Correct 3 ms 596 KB n = 2
3 Correct 3 ms 600 KB n = 2
4 Correct 2 ms 700 KB n = 2
5 Correct 2 ms 700 KB n = 2
6 Correct 3 ms 700 KB n = 2
7 Correct 2 ms 700 KB n = 3
8 Correct 4 ms 700 KB n = 3
9 Correct 2 ms 700 KB n = 3
10 Correct 2 ms 700 KB n = 8
11 Correct 2 ms 700 KB n = 8
12 Correct 2 ms 700 KB n = 8
13 Correct 2 ms 700 KB n = 8
14 Correct 2 ms 700 KB n = 8
15 Correct 2 ms 700 KB n = 8
16 Correct 2 ms 700 KB n = 8
17 Correct 2 ms 700 KB n = 8
18 Correct 2 ms 776 KB n = 8
19 Correct 2 ms 776 KB n = 3
20 Correct 2 ms 776 KB n = 7
21 Correct 2 ms 788 KB n = 8
22 Correct 2 ms 792 KB n = 8
23 Correct 2 ms 792 KB n = 8
24 Correct 2 ms 792 KB n = 8
25 Correct 2 ms 792 KB n = 8
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB n = 2
2 Correct 3 ms 596 KB n = 2
3 Correct 3 ms 600 KB n = 2
4 Correct 2 ms 700 KB n = 2
5 Correct 2 ms 700 KB n = 2
6 Correct 3 ms 700 KB n = 2
7 Correct 2 ms 700 KB n = 3
8 Correct 4 ms 700 KB n = 3
9 Correct 2 ms 700 KB n = 3
10 Correct 2 ms 700 KB n = 8
11 Correct 2 ms 700 KB n = 8
12 Correct 2 ms 700 KB n = 8
13 Correct 2 ms 700 KB n = 8
14 Correct 2 ms 700 KB n = 8
15 Correct 2 ms 700 KB n = 8
16 Correct 2 ms 700 KB n = 8
17 Correct 2 ms 700 KB n = 8
18 Correct 2 ms 776 KB n = 8
19 Correct 2 ms 776 KB n = 3
20 Correct 2 ms 776 KB n = 7
21 Correct 2 ms 788 KB n = 8
22 Correct 2 ms 792 KB n = 8
23 Correct 2 ms 792 KB n = 8
24 Correct 2 ms 792 KB n = 8
25 Correct 2 ms 792 KB n = 8
26 Correct 2 ms 792 KB n = 8
27 Correct 2 ms 792 KB n = 8
28 Correct 2 ms 816 KB n = 8
29 Correct 2 ms 816 KB n = 16
30 Correct 2 ms 816 KB n = 16
31 Correct 2 ms 816 KB n = 16
32 Correct 2 ms 816 KB n = 16
33 Correct 2 ms 816 KB n = 16
34 Correct 2 ms 816 KB n = 16
35 Correct 2 ms 816 KB n = 16
36 Correct 2 ms 816 KB n = 15
37 Correct 2 ms 816 KB n = 8
38 Correct 2 ms 816 KB n = 16
39 Correct 2 ms 816 KB n = 16
40 Correct 2 ms 816 KB n = 9
41 Correct 2 ms 816 KB n = 16
42 Correct 2 ms 816 KB n = 16
43 Correct 2 ms 816 KB n = 16
44 Correct 2 ms 816 KB n = 9
45 Correct 3 ms 816 KB n = 15
46 Correct 2 ms 816 KB n = 16
47 Correct 2 ms 816 KB n = 16
48 Correct 3 ms 816 KB n = 16
# Verdict Execution time Memory Grader output
1 Correct 278 ms 16404 KB n = 199999
2 Correct 294 ms 16404 KB n = 199991
3 Correct 330 ms 16404 KB n = 199993
4 Correct 216 ms 16404 KB n = 152076
5 Correct 152 ms 16404 KB n = 93249
6 Correct 236 ms 16404 KB n = 199910
7 Correct 237 ms 16804 KB n = 199999
8 Correct 215 ms 16804 KB n = 199997
9 Correct 251 ms 16804 KB n = 171294
10 Correct 191 ms 16804 KB n = 140872
11 Correct 280 ms 16804 KB n = 199886
12 Correct 248 ms 16804 KB n = 199996
13 Correct 254 ms 16804 KB n = 200000
14 Correct 277 ms 18052 KB n = 199998
15 Correct 236 ms 18052 KB n = 200000
16 Correct 254 ms 18052 KB n = 199998
17 Correct 267 ms 19364 KB n = 200000
18 Correct 300 ms 19364 KB n = 190000
19 Correct 222 ms 19364 KB n = 177777
20 Correct 130 ms 19364 KB n = 100000
21 Correct 270 ms 19364 KB n = 200000
22 Correct 262 ms 19364 KB n = 200000
23 Correct 278 ms 19364 KB n = 200000
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB n = 2
2 Correct 3 ms 596 KB n = 2
3 Correct 3 ms 600 KB n = 2
4 Correct 2 ms 700 KB n = 2
5 Correct 2 ms 700 KB n = 2
6 Correct 3 ms 700 KB n = 2
7 Correct 2 ms 700 KB n = 3
8 Correct 4 ms 700 KB n = 3
9 Correct 2 ms 700 KB n = 3
10 Correct 2 ms 700 KB n = 8
11 Correct 2 ms 700 KB n = 8
12 Correct 2 ms 700 KB n = 8
13 Correct 2 ms 700 KB n = 8
14 Correct 2 ms 700 KB n = 8
15 Correct 2 ms 700 KB n = 8
16 Correct 2 ms 700 KB n = 8
17 Correct 2 ms 700 KB n = 8
18 Correct 2 ms 776 KB n = 8
19 Correct 2 ms 776 KB n = 3
20 Correct 2 ms 776 KB n = 7
21 Correct 2 ms 788 KB n = 8
22 Correct 2 ms 792 KB n = 8
23 Correct 2 ms 792 KB n = 8
24 Correct 2 ms 792 KB n = 8
25 Correct 2 ms 792 KB n = 8
26 Correct 2 ms 792 KB n = 8
27 Correct 2 ms 792 KB n = 8
28 Correct 2 ms 816 KB n = 8
29 Correct 2 ms 816 KB n = 16
30 Correct 2 ms 816 KB n = 16
31 Correct 2 ms 816 KB n = 16
32 Correct 2 ms 816 KB n = 16
33 Correct 2 ms 816 KB n = 16
34 Correct 2 ms 816 KB n = 16
35 Correct 2 ms 816 KB n = 16
36 Correct 2 ms 816 KB n = 15
37 Correct 2 ms 816 KB n = 8
38 Correct 2 ms 816 KB n = 16
39 Correct 2 ms 816 KB n = 16
40 Correct 2 ms 816 KB n = 9
41 Correct 2 ms 816 KB n = 16
42 Correct 2 ms 816 KB n = 16
43 Correct 2 ms 816 KB n = 16
44 Correct 2 ms 816 KB n = 9
45 Correct 3 ms 816 KB n = 15
46 Correct 2 ms 816 KB n = 16
47 Correct 2 ms 816 KB n = 16
48 Correct 3 ms 816 KB n = 16
49 Correct 278 ms 16404 KB n = 199999
50 Correct 294 ms 16404 KB n = 199991
51 Correct 330 ms 16404 KB n = 199993
52 Correct 216 ms 16404 KB n = 152076
53 Correct 152 ms 16404 KB n = 93249
54 Correct 236 ms 16404 KB n = 199910
55 Correct 237 ms 16804 KB n = 199999
56 Correct 215 ms 16804 KB n = 199997
57 Correct 251 ms 16804 KB n = 171294
58 Correct 191 ms 16804 KB n = 140872
59 Correct 280 ms 16804 KB n = 199886
60 Correct 248 ms 16804 KB n = 199996
61 Correct 254 ms 16804 KB n = 200000
62 Correct 277 ms 18052 KB n = 199998
63 Correct 236 ms 18052 KB n = 200000
64 Correct 254 ms 18052 KB n = 199998
65 Correct 267 ms 19364 KB n = 200000
66 Correct 300 ms 19364 KB n = 190000
67 Correct 222 ms 19364 KB n = 177777
68 Correct 130 ms 19364 KB n = 100000
69 Correct 270 ms 19364 KB n = 200000
70 Correct 262 ms 19364 KB n = 200000
71 Correct 278 ms 19364 KB n = 200000
72 Correct 285 ms 20164 KB n = 200000
73 Correct 293 ms 24060 KB n = 200000
74 Correct 312 ms 28140 KB n = 200000
75 Correct 293 ms 31836 KB n = 200000
76 Correct 306 ms 35728 KB n = 200000
77 Correct 207 ms 38096 KB n = 200000
78 Correct 222 ms 38976 KB n = 200000
79 Correct 247 ms 46312 KB n = 184307
80 Correct 127 ms 46312 KB n = 76040
81 Correct 223 ms 50604 KB n = 199981
82 Correct 243 ms 54892 KB n = 199994
83 Correct 262 ms 57356 KB n = 199996
84 Correct 291 ms 62112 KB n = 199998
85 Correct 273 ms 64408 KB n = 200000
86 Correct 357 ms 67700 KB n = 199998
87 Correct 303 ms 73760 KB n = 200000
88 Correct 373 ms 75096 KB n = 200000
89 Correct 339 ms 81464 KB n = 200000
90 Correct 329 ms 82200 KB n = 200000
91 Correct 323 ms 86092 KB n = 200000
92 Correct 260 ms 89932 KB n = 200000