Submission #137331

# Submission time Handle Problem Language Result Execution time Memory
137331 2019-07-27T13:09:30 Z choikiwon Ancient Books (IOI17_books) C++17
100 / 100
511 ms 38736 KB
#include "books.h"
#include<bits/stdc++.h>
using namespace std;

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

const int maxn = 1000010;

int N, S;
int A[maxn], vis[maxn];
vector<pii> V;

int fa[maxn], le[maxn], ri[maxn];
void init() {
    for(int i = 0; i < N; i++) {
        fa[i] = i;
        le[i] = i;
        ri[i] = i;
    }
}
int find(int u) {
    if(fa[u] == u) return u;
    else return fa[u] = find(fa[u]);
}
void mrg(int u, int v) {
    u = find(u);
    v = find(v);
    if(u == v) return;
    fa[v] = u;
    le[u] = min(le[u], le[v]);
    ri[u] = max(ri[u], ri[v]);
}

ll minimum_walk(std::vector<int> p, int s) {
	N = p.size();
	S = s;
	for(int i = 0; i < N; i++) {
        A[i] = p[i];
	}

	init();

	ll ret = 0;
	for(int i = 0; i < N; i++) {
        if(vis[i]) continue;
        if(A[i] == i) {
            if(i == S) V.push_back(pii(S, S));
            continue;
        }

        int mx = -1e9;

        int u = i;
        while(!vis[u]) {
            vis[u] = 1;
            mx = max(mx, u);

            ret += abs(u - A[u]);
            mrg(u, A[u]);
            u = A[u];
        }

        V.push_back(pii(i, mx));
	}

	if(V.size() == 0) return ret;

	int st = V[0].first;
	int ed = V[0].second;

	pii dest;
	for(int i = 1; i < V.size(); i++) {
        if(ed < V[i].first) {
            ret += 2 * (V[i].first - ed);

            if(st <= S && S <= ed) {
                dest = pii(st, ed);
            }

            st = V[i].first;
            ed = V[i].second;
            continue;
        }
        ed = max(ed, V[i].second);
	}

	if(st <= S && S <= ed) {
        dest = pii(st, ed);
	}

	int l = S;
	int r = S;
	int mn = 1e9;
	int mx = -1e9;
	while(l != dest.first || r != dest.second) {
        mn = min(mn, le[find(l)]);
        mn = min(mn, le[find(r)]);
        mx = max(mx, ri[find(l)]);
        mx = max(mx, ri[find(r)]);

        if(mn == l && mx == r) {
            l--;
            r++;
            ret += 2;
            continue;
        }
        if(mn < l) {
            l--;
            continue;
        }
        if(r < mx) {
            r++;
            continue;
        }
	}

	return ret;
}

Compilation message

books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:73:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 1; i < V.size(); i++) {
                 ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 511 ms 29060 KB Output is correct
31 Correct 427 ms 29020 KB Output is correct
32 Correct 145 ms 25080 KB Output is correct
33 Correct 164 ms 30620 KB Output is correct
34 Correct 167 ms 30592 KB Output is correct
35 Correct 169 ms 30772 KB Output is correct
36 Correct 169 ms 30540 KB Output is correct
37 Correct 169 ms 29272 KB Output is correct
38 Correct 176 ms 29100 KB Output is correct
39 Correct 179 ms 29024 KB Output is correct
40 Correct 210 ms 29048 KB Output is correct
41 Correct 273 ms 29020 KB Output is correct
42 Correct 241 ms 29048 KB Output is correct
43 Correct 181 ms 31080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 372 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 380 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 511 ms 29060 KB Output is correct
31 Correct 427 ms 29020 KB Output is correct
32 Correct 145 ms 25080 KB Output is correct
33 Correct 164 ms 30620 KB Output is correct
34 Correct 167 ms 30592 KB Output is correct
35 Correct 169 ms 30772 KB Output is correct
36 Correct 169 ms 30540 KB Output is correct
37 Correct 169 ms 29272 KB Output is correct
38 Correct 176 ms 29100 KB Output is correct
39 Correct 179 ms 29024 KB Output is correct
40 Correct 210 ms 29048 KB Output is correct
41 Correct 273 ms 29020 KB Output is correct
42 Correct 241 ms 29048 KB Output is correct
43 Correct 181 ms 31080 KB Output is correct
44 Correct 2 ms 372 KB Output is correct
45 Correct 2 ms 376 KB Output is correct
46 Correct 2 ms 376 KB Output is correct
47 Correct 2 ms 376 KB Output is correct
48 Correct 2 ms 376 KB Output is correct
49 Correct 2 ms 376 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 376 KB Output is correct
52 Correct 3 ms 376 KB Output is correct
53 Correct 3 ms 376 KB Output is correct
54 Correct 2 ms 376 KB Output is correct
55 Correct 2 ms 376 KB Output is correct
56 Correct 2 ms 376 KB Output is correct
57 Correct 2 ms 376 KB Output is correct
58 Correct 2 ms 380 KB Output is correct
59 Correct 2 ms 376 KB Output is correct
60 Correct 2 ms 376 KB Output is correct
61 Correct 2 ms 376 KB Output is correct
62 Correct 2 ms 376 KB Output is correct
63 Correct 2 ms 376 KB Output is correct
64 Correct 178 ms 35380 KB Output is correct
65 Correct 172 ms 35412 KB Output is correct
66 Correct 187 ms 34748 KB Output is correct
67 Correct 185 ms 34680 KB Output is correct
68 Correct 19 ms 3960 KB Output is correct
69 Correct 19 ms 3832 KB Output is correct
70 Correct 19 ms 3832 KB Output is correct
71 Correct 19 ms 3960 KB Output is correct
72 Correct 18 ms 3960 KB Output is correct
73 Correct 21 ms 3704 KB Output is correct
74 Correct 164 ms 36200 KB Output is correct
75 Correct 163 ms 36076 KB Output is correct
76 Correct 199 ms 36716 KB Output is correct
77 Correct 183 ms 36584 KB Output is correct
78 Correct 184 ms 37584 KB Output is correct
79 Correct 183 ms 37580 KB Output is correct
80 Correct 148 ms 30560 KB Output is correct
81 Correct 170 ms 38496 KB Output is correct
82 Correct 175 ms 38468 KB Output is correct
83 Correct 185 ms 36528 KB Output is correct
84 Correct 184 ms 36656 KB Output is correct
85 Correct 181 ms 35568 KB Output is correct
86 Correct 197 ms 35128 KB Output is correct
87 Correct 180 ms 36076 KB Output is correct
88 Correct 184 ms 38632 KB Output is correct
89 Correct 188 ms 38736 KB Output is correct
90 Correct 184 ms 34908 KB Output is correct
91 Correct 187 ms 34580 KB Output is correct
92 Correct 207 ms 34524 KB Output is correct