Submission #400970

# Submission time Handle Problem Language Result Execution time Memory
400970 2021-05-08T22:53:15 Z LucaDantas Ancient Books (IOI17_books) C++17
100 / 100
555 ms 151764 KB
#include "books.h"
#include <cmath>
#include <queue>
#include <vector>
#include <utility>
#include <algorithm>
#include <cassert>

constexpr int maxn = 2e6+10;

int t, comp[maxn], dist[maxn], L[maxn], R[maxn], opa[maxn];
std::vector<std::pair<int,int>> g[maxn];

bool foi[maxn];

void bfs(int s) {
	for(int i = 0; i < maxn; i++)
		dist[i] = maxn;
	std::deque<int> q = {s};
	dist[s] = 0;
	while(q.size()) {
		int u = q.front(); q.pop_front();
		if(foi[u]) continue;
		foi[u] = 1;
		for(auto pp : g[u]) {
			auto [v, w] = pp;
			if(dist[v] <= dist[u] + w) continue;
			dist[v] = dist[u] + w;
			if(w) q.push_back(v);
			else q.push_front(v);
		}
	}
}

void dfs(int u, const std::vector<int>& p, int& fim) {
	fim = std::max(fim, u);
	comp[u] = t;
	if(!comp[p[u]]) dfs(p[u], p, fim);
}

int pai[maxn], n;
bool mark[maxn];

int pr(int u) { return !mark[u] || u==n-1 ? u : pai[u] = pr(pai[u]); }

long long minimum_walk(std::vector<int> p, int s) {
	n = (int)p.size();
	long long ans = 0;

	std::vector<std::pair<int,int>> seg;

	int tot = 0;
	for(int i = 0; i < n; i++) {
		pai[i] = i+1;
		tot = std::max(tot, p[i]);
		opa[i] = tot>i;
		ans += abs(i-p[i]);
		if(!comp[i] && tot > i) {
			int fim = 0; ++t;
			dfs(i, p, fim);
			seg.push_back({i, fim});
			L[t] = i, R[t] = fim;
		}
	}

	std::sort(seg.begin(), seg.end(), [](std::pair<int,int> x, std::pair<int,int> y) {
		return x.second - x.first < y.second - y.first;
	});

	for(std::pair<int,int> p : seg) {
		auto [l, r] = p;
		for(int i = l; i < r; i = pr(i)) {
			if(L[comp[i]] < l || R[comp[i]] > r)
				g[comp[l]].push_back({comp[i], 0}), g[comp[i]].push_back({comp[l], 0});
			mark[i] = 1;
		}
		mark[l] = mark[r] = 0;
	}

	for(int i = 1; i < n; i++) if(opa[i-1] && comp[i] != comp[i-1])
		g[comp[i]].push_back({comp[i-1], 1}), g[comp[i-1]].push_back({comp[i], 1});


	if(comp[s]) {
		bfs(comp[s]);
		for(int i = 1; i <= t; i++)
			if(dist[i] < maxn) { ans += dist[i]<<1; break; }
	}

	int cnt = 0;
	for(int i = s-1; i >= 0; i--)
		if(!opa[i]) ++cnt;
		else ans += cnt<<1, cnt = 0;

	cnt = 0;
	for(int i = s; i < n; i++)
		if(!opa[i]) ++cnt;
		else ans += cnt<<1, cnt = 0;

	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 25 ms 47288 KB Output is correct
2 Correct 26 ms 47192 KB Output is correct
3 Correct 33 ms 55072 KB Output is correct
4 Correct 32 ms 55052 KB Output is correct
5 Correct 31 ms 55044 KB Output is correct
6 Correct 33 ms 55068 KB Output is correct
7 Correct 32 ms 55076 KB Output is correct
8 Correct 33 ms 55040 KB Output is correct
9 Correct 27 ms 47272 KB Output is correct
10 Correct 27 ms 47380 KB Output is correct
11 Correct 31 ms 47160 KB Output is correct
12 Correct 32 ms 55120 KB Output is correct
13 Correct 32 ms 55068 KB Output is correct
14 Correct 27 ms 47180 KB Output is correct
15 Correct 27 ms 47212 KB Output is correct
16 Correct 34 ms 55028 KB Output is correct
17 Correct 34 ms 55100 KB Output is correct
18 Correct 33 ms 55056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 47288 KB Output is correct
2 Correct 26 ms 47192 KB Output is correct
3 Correct 33 ms 55072 KB Output is correct
4 Correct 32 ms 55052 KB Output is correct
5 Correct 31 ms 55044 KB Output is correct
6 Correct 33 ms 55068 KB Output is correct
7 Correct 32 ms 55076 KB Output is correct
8 Correct 33 ms 55040 KB Output is correct
9 Correct 27 ms 47272 KB Output is correct
10 Correct 27 ms 47380 KB Output is correct
11 Correct 31 ms 47160 KB Output is correct
12 Correct 32 ms 55120 KB Output is correct
13 Correct 32 ms 55068 KB Output is correct
14 Correct 27 ms 47180 KB Output is correct
15 Correct 27 ms 47212 KB Output is correct
16 Correct 34 ms 55028 KB Output is correct
17 Correct 34 ms 55100 KB Output is correct
18 Correct 33 ms 55056 KB Output is correct
19 Correct 37 ms 55116 KB Output is correct
20 Correct 35 ms 55140 KB Output is correct
21 Correct 27 ms 47180 KB Output is correct
22 Correct 38 ms 55124 KB Output is correct
23 Correct 27 ms 47284 KB Output is correct
24 Correct 28 ms 47248 KB Output is correct
25 Correct 28 ms 47308 KB Output is correct
26 Correct 33 ms 55136 KB Output is correct
27 Correct 33 ms 55172 KB Output is correct
28 Correct 32 ms 55116 KB Output is correct
29 Correct 33 ms 55108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 47288 KB Output is correct
2 Correct 26 ms 47192 KB Output is correct
3 Correct 33 ms 55072 KB Output is correct
4 Correct 32 ms 55052 KB Output is correct
5 Correct 31 ms 55044 KB Output is correct
6 Correct 33 ms 55068 KB Output is correct
7 Correct 32 ms 55076 KB Output is correct
8 Correct 33 ms 55040 KB Output is correct
9 Correct 27 ms 47272 KB Output is correct
10 Correct 27 ms 47380 KB Output is correct
11 Correct 31 ms 47160 KB Output is correct
12 Correct 32 ms 55120 KB Output is correct
13 Correct 32 ms 55068 KB Output is correct
14 Correct 27 ms 47180 KB Output is correct
15 Correct 27 ms 47212 KB Output is correct
16 Correct 34 ms 55028 KB Output is correct
17 Correct 34 ms 55100 KB Output is correct
18 Correct 33 ms 55056 KB Output is correct
19 Correct 37 ms 55116 KB Output is correct
20 Correct 35 ms 55140 KB Output is correct
21 Correct 27 ms 47180 KB Output is correct
22 Correct 38 ms 55124 KB Output is correct
23 Correct 27 ms 47284 KB Output is correct
24 Correct 28 ms 47248 KB Output is correct
25 Correct 28 ms 47308 KB Output is correct
26 Correct 33 ms 55136 KB Output is correct
27 Correct 33 ms 55172 KB Output is correct
28 Correct 32 ms 55116 KB Output is correct
29 Correct 33 ms 55108 KB Output is correct
30 Correct 311 ms 136532 KB Output is correct
31 Correct 364 ms 151764 KB Output is correct
32 Correct 168 ms 69544 KB Output is correct
33 Correct 221 ms 81992 KB Output is correct
34 Correct 252 ms 82112 KB Output is correct
35 Correct 258 ms 94536 KB Output is correct
36 Correct 314 ms 104344 KB Output is correct
37 Correct 261 ms 114240 KB Output is correct
38 Correct 242 ms 113964 KB Output is correct
39 Correct 248 ms 114180 KB Output is correct
40 Correct 237 ms 110836 KB Output is correct
41 Correct 273 ms 120268 KB Output is correct
42 Correct 268 ms 117752 KB Output is correct
43 Correct 491 ms 122272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 55176 KB Output is correct
2 Correct 35 ms 55180 KB Output is correct
3 Correct 33 ms 55176 KB Output is correct
4 Correct 28 ms 47220 KB Output is correct
5 Correct 33 ms 55164 KB Output is correct
6 Correct 38 ms 55108 KB Output is correct
7 Correct 33 ms 55204 KB Output is correct
8 Correct 34 ms 55176 KB Output is correct
9 Correct 33 ms 55188 KB Output is correct
10 Correct 28 ms 47268 KB Output is correct
11 Correct 33 ms 55212 KB Output is correct
12 Correct 33 ms 55108 KB Output is correct
13 Correct 33 ms 55188 KB Output is correct
14 Correct 34 ms 55068 KB Output is correct
15 Correct 34 ms 55160 KB Output is correct
16 Correct 38 ms 55108 KB Output is correct
17 Correct 35 ms 55168 KB Output is correct
18 Correct 33 ms 55184 KB Output is correct
19 Correct 33 ms 55084 KB Output is correct
20 Correct 34 ms 55100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 47288 KB Output is correct
2 Correct 26 ms 47192 KB Output is correct
3 Correct 33 ms 55072 KB Output is correct
4 Correct 32 ms 55052 KB Output is correct
5 Correct 31 ms 55044 KB Output is correct
6 Correct 33 ms 55068 KB Output is correct
7 Correct 32 ms 55076 KB Output is correct
8 Correct 33 ms 55040 KB Output is correct
9 Correct 27 ms 47272 KB Output is correct
10 Correct 27 ms 47380 KB Output is correct
11 Correct 31 ms 47160 KB Output is correct
12 Correct 32 ms 55120 KB Output is correct
13 Correct 32 ms 55068 KB Output is correct
14 Correct 27 ms 47180 KB Output is correct
15 Correct 27 ms 47212 KB Output is correct
16 Correct 34 ms 55028 KB Output is correct
17 Correct 34 ms 55100 KB Output is correct
18 Correct 33 ms 55056 KB Output is correct
19 Correct 37 ms 55116 KB Output is correct
20 Correct 35 ms 55140 KB Output is correct
21 Correct 27 ms 47180 KB Output is correct
22 Correct 38 ms 55124 KB Output is correct
23 Correct 27 ms 47284 KB Output is correct
24 Correct 28 ms 47248 KB Output is correct
25 Correct 28 ms 47308 KB Output is correct
26 Correct 33 ms 55136 KB Output is correct
27 Correct 33 ms 55172 KB Output is correct
28 Correct 32 ms 55116 KB Output is correct
29 Correct 33 ms 55108 KB Output is correct
30 Correct 311 ms 136532 KB Output is correct
31 Correct 364 ms 151764 KB Output is correct
32 Correct 168 ms 69544 KB Output is correct
33 Correct 221 ms 81992 KB Output is correct
34 Correct 252 ms 82112 KB Output is correct
35 Correct 258 ms 94536 KB Output is correct
36 Correct 314 ms 104344 KB Output is correct
37 Correct 261 ms 114240 KB Output is correct
38 Correct 242 ms 113964 KB Output is correct
39 Correct 248 ms 114180 KB Output is correct
40 Correct 237 ms 110836 KB Output is correct
41 Correct 273 ms 120268 KB Output is correct
42 Correct 268 ms 117752 KB Output is correct
43 Correct 491 ms 122272 KB Output is correct
44 Correct 33 ms 55176 KB Output is correct
45 Correct 35 ms 55180 KB Output is correct
46 Correct 33 ms 55176 KB Output is correct
47 Correct 28 ms 47220 KB Output is correct
48 Correct 33 ms 55164 KB Output is correct
49 Correct 38 ms 55108 KB Output is correct
50 Correct 33 ms 55204 KB Output is correct
51 Correct 34 ms 55176 KB Output is correct
52 Correct 33 ms 55188 KB Output is correct
53 Correct 28 ms 47268 KB Output is correct
54 Correct 33 ms 55212 KB Output is correct
55 Correct 33 ms 55108 KB Output is correct
56 Correct 33 ms 55188 KB Output is correct
57 Correct 34 ms 55068 KB Output is correct
58 Correct 34 ms 55160 KB Output is correct
59 Correct 38 ms 55108 KB Output is correct
60 Correct 35 ms 55168 KB Output is correct
61 Correct 33 ms 55184 KB Output is correct
62 Correct 33 ms 55084 KB Output is correct
63 Correct 34 ms 55100 KB Output is correct
64 Correct 392 ms 120816 KB Output is correct
65 Correct 421 ms 122808 KB Output is correct
66 Correct 273 ms 114140 KB Output is correct
67 Correct 260 ms 114900 KB Output is correct
68 Correct 60 ms 60608 KB Output is correct
69 Correct 58 ms 60564 KB Output is correct
70 Correct 62 ms 60880 KB Output is correct
71 Correct 75 ms 61192 KB Output is correct
72 Correct 68 ms 61936 KB Output is correct
73 Correct 53 ms 61168 KB Output is correct
74 Correct 225 ms 89800 KB Output is correct
75 Correct 222 ms 82072 KB Output is correct
76 Correct 555 ms 122416 KB Output is correct
77 Correct 531 ms 122364 KB Output is correct
78 Correct 413 ms 116920 KB Output is correct
79 Correct 414 ms 116956 KB Output is correct
80 Correct 173 ms 69828 KB Output is correct
81 Correct 295 ms 122056 KB Output is correct
82 Correct 307 ms 122132 KB Output is correct
83 Correct 341 ms 107252 KB Output is correct
84 Correct 356 ms 107668 KB Output is correct
85 Correct 341 ms 110132 KB Output is correct
86 Correct 301 ms 113860 KB Output is correct
87 Correct 358 ms 125316 KB Output is correct
88 Correct 327 ms 117556 KB Output is correct
89 Correct 327 ms 108592 KB Output is correct
90 Correct 269 ms 116444 KB Output is correct
91 Correct 257 ms 115260 KB Output is correct
92 Correct 245 ms 111056 KB Output is correct