Submission #469070

# Submission time Handle Problem Language Result Execution time Memory
469070 2021-08-30T15:41:39 Z LucaDantas Ancient Books (IOI17_books) C++17
100 / 100
586 ms 136432 KB
#include "books.h"
#include <cmath>
#include <queue>
#include <vector>
#include <utility>
#include <algorithm>
#include <cassert>
#include <cstdio>

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)) {
			// this bit was incorrect even though it was accepted on the test cases, but this thing I added should fix it
			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}), L[comp[l]] = std::min(L[comp[l]], L[comp[i]]), R[comp[l]] = std::max(R[comp[l]], R[comp[i]]);
			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 32 ms 47272 KB Output is correct
2 Correct 31 ms 47284 KB Output is correct
3 Correct 36 ms 55092 KB Output is correct
4 Correct 35 ms 55144 KB Output is correct
5 Correct 39 ms 55008 KB Output is correct
6 Correct 36 ms 55116 KB Output is correct
7 Correct 35 ms 55112 KB Output is correct
8 Correct 36 ms 55024 KB Output is correct
9 Correct 31 ms 47260 KB Output is correct
10 Correct 30 ms 47180 KB Output is correct
11 Correct 30 ms 47156 KB Output is correct
12 Correct 37 ms 55144 KB Output is correct
13 Correct 36 ms 55076 KB Output is correct
14 Correct 30 ms 47236 KB Output is correct
15 Correct 33 ms 47280 KB Output is correct
16 Correct 35 ms 55084 KB Output is correct
17 Correct 35 ms 55092 KB Output is correct
18 Correct 36 ms 54992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 47272 KB Output is correct
2 Correct 31 ms 47284 KB Output is correct
3 Correct 36 ms 55092 KB Output is correct
4 Correct 35 ms 55144 KB Output is correct
5 Correct 39 ms 55008 KB Output is correct
6 Correct 36 ms 55116 KB Output is correct
7 Correct 35 ms 55112 KB Output is correct
8 Correct 36 ms 55024 KB Output is correct
9 Correct 31 ms 47260 KB Output is correct
10 Correct 30 ms 47180 KB Output is correct
11 Correct 30 ms 47156 KB Output is correct
12 Correct 37 ms 55144 KB Output is correct
13 Correct 36 ms 55076 KB Output is correct
14 Correct 30 ms 47236 KB Output is correct
15 Correct 33 ms 47280 KB Output is correct
16 Correct 35 ms 55084 KB Output is correct
17 Correct 35 ms 55092 KB Output is correct
18 Correct 36 ms 54992 KB Output is correct
19 Correct 36 ms 55116 KB Output is correct
20 Correct 39 ms 55304 KB Output is correct
21 Correct 31 ms 47248 KB Output is correct
22 Correct 37 ms 55148 KB Output is correct
23 Correct 32 ms 47260 KB Output is correct
24 Correct 31 ms 47240 KB Output is correct
25 Correct 34 ms 47312 KB Output is correct
26 Correct 35 ms 55188 KB Output is correct
27 Correct 35 ms 55116 KB Output is correct
28 Correct 36 ms 55168 KB Output is correct
29 Correct 36 ms 55080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 47272 KB Output is correct
2 Correct 31 ms 47284 KB Output is correct
3 Correct 36 ms 55092 KB Output is correct
4 Correct 35 ms 55144 KB Output is correct
5 Correct 39 ms 55008 KB Output is correct
6 Correct 36 ms 55116 KB Output is correct
7 Correct 35 ms 55112 KB Output is correct
8 Correct 36 ms 55024 KB Output is correct
9 Correct 31 ms 47260 KB Output is correct
10 Correct 30 ms 47180 KB Output is correct
11 Correct 30 ms 47156 KB Output is correct
12 Correct 37 ms 55144 KB Output is correct
13 Correct 36 ms 55076 KB Output is correct
14 Correct 30 ms 47236 KB Output is correct
15 Correct 33 ms 47280 KB Output is correct
16 Correct 35 ms 55084 KB Output is correct
17 Correct 35 ms 55092 KB Output is correct
18 Correct 36 ms 54992 KB Output is correct
19 Correct 36 ms 55116 KB Output is correct
20 Correct 39 ms 55304 KB Output is correct
21 Correct 31 ms 47248 KB Output is correct
22 Correct 37 ms 55148 KB Output is correct
23 Correct 32 ms 47260 KB Output is correct
24 Correct 31 ms 47240 KB Output is correct
25 Correct 34 ms 47312 KB Output is correct
26 Correct 35 ms 55188 KB Output is correct
27 Correct 35 ms 55116 KB Output is correct
28 Correct 36 ms 55168 KB Output is correct
29 Correct 36 ms 55080 KB Output is correct
30 Correct 306 ms 121044 KB Output is correct
31 Correct 304 ms 136432 KB Output is correct
32 Correct 167 ms 69708 KB Output is correct
33 Correct 231 ms 82136 KB Output is correct
34 Correct 275 ms 82228 KB Output is correct
35 Correct 265 ms 95168 KB Output is correct
36 Correct 348 ms 106816 KB Output is correct
37 Correct 269 ms 116368 KB Output is correct
38 Correct 243 ms 114524 KB Output is correct
39 Correct 246 ms 114396 KB Output is correct
40 Correct 240 ms 110484 KB Output is correct
41 Correct 270 ms 114728 KB Output is correct
42 Correct 253 ms 114328 KB Output is correct
43 Correct 487 ms 122344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 55072 KB Output is correct
2 Correct 35 ms 55116 KB Output is correct
3 Correct 37 ms 55116 KB Output is correct
4 Correct 33 ms 47300 KB Output is correct
5 Correct 35 ms 55152 KB Output is correct
6 Correct 36 ms 55116 KB Output is correct
7 Correct 36 ms 55116 KB Output is correct
8 Correct 36 ms 55052 KB Output is correct
9 Correct 36 ms 55092 KB Output is correct
10 Correct 31 ms 47280 KB Output is correct
11 Correct 36 ms 55124 KB Output is correct
12 Correct 35 ms 55108 KB Output is correct
13 Correct 36 ms 55072 KB Output is correct
14 Correct 40 ms 55064 KB Output is correct
15 Correct 36 ms 55120 KB Output is correct
16 Correct 35 ms 55160 KB Output is correct
17 Correct 37 ms 55092 KB Output is correct
18 Correct 33 ms 55092 KB Output is correct
19 Correct 35 ms 55168 KB Output is correct
20 Correct 40 ms 55148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 47272 KB Output is correct
2 Correct 31 ms 47284 KB Output is correct
3 Correct 36 ms 55092 KB Output is correct
4 Correct 35 ms 55144 KB Output is correct
5 Correct 39 ms 55008 KB Output is correct
6 Correct 36 ms 55116 KB Output is correct
7 Correct 35 ms 55112 KB Output is correct
8 Correct 36 ms 55024 KB Output is correct
9 Correct 31 ms 47260 KB Output is correct
10 Correct 30 ms 47180 KB Output is correct
11 Correct 30 ms 47156 KB Output is correct
12 Correct 37 ms 55144 KB Output is correct
13 Correct 36 ms 55076 KB Output is correct
14 Correct 30 ms 47236 KB Output is correct
15 Correct 33 ms 47280 KB Output is correct
16 Correct 35 ms 55084 KB Output is correct
17 Correct 35 ms 55092 KB Output is correct
18 Correct 36 ms 54992 KB Output is correct
19 Correct 36 ms 55116 KB Output is correct
20 Correct 39 ms 55304 KB Output is correct
21 Correct 31 ms 47248 KB Output is correct
22 Correct 37 ms 55148 KB Output is correct
23 Correct 32 ms 47260 KB Output is correct
24 Correct 31 ms 47240 KB Output is correct
25 Correct 34 ms 47312 KB Output is correct
26 Correct 35 ms 55188 KB Output is correct
27 Correct 35 ms 55116 KB Output is correct
28 Correct 36 ms 55168 KB Output is correct
29 Correct 36 ms 55080 KB Output is correct
30 Correct 306 ms 121044 KB Output is correct
31 Correct 304 ms 136432 KB Output is correct
32 Correct 167 ms 69708 KB Output is correct
33 Correct 231 ms 82136 KB Output is correct
34 Correct 275 ms 82228 KB Output is correct
35 Correct 265 ms 95168 KB Output is correct
36 Correct 348 ms 106816 KB Output is correct
37 Correct 269 ms 116368 KB Output is correct
38 Correct 243 ms 114524 KB Output is correct
39 Correct 246 ms 114396 KB Output is correct
40 Correct 240 ms 110484 KB Output is correct
41 Correct 270 ms 114728 KB Output is correct
42 Correct 253 ms 114328 KB Output is correct
43 Correct 487 ms 122344 KB Output is correct
44 Correct 35 ms 55072 KB Output is correct
45 Correct 35 ms 55116 KB Output is correct
46 Correct 37 ms 55116 KB Output is correct
47 Correct 33 ms 47300 KB Output is correct
48 Correct 35 ms 55152 KB Output is correct
49 Correct 36 ms 55116 KB Output is correct
50 Correct 36 ms 55116 KB Output is correct
51 Correct 36 ms 55052 KB Output is correct
52 Correct 36 ms 55092 KB Output is correct
53 Correct 31 ms 47280 KB Output is correct
54 Correct 36 ms 55124 KB Output is correct
55 Correct 35 ms 55108 KB Output is correct
56 Correct 36 ms 55072 KB Output is correct
57 Correct 40 ms 55064 KB Output is correct
58 Correct 36 ms 55120 KB Output is correct
59 Correct 35 ms 55160 KB Output is correct
60 Correct 37 ms 55092 KB Output is correct
61 Correct 33 ms 55092 KB Output is correct
62 Correct 35 ms 55168 KB Output is correct
63 Correct 40 ms 55148 KB Output is correct
64 Correct 385 ms 120748 KB Output is correct
65 Correct 382 ms 122836 KB Output is correct
66 Correct 269 ms 115900 KB Output is correct
67 Correct 257 ms 116248 KB Output is correct
68 Correct 71 ms 60736 KB Output is correct
69 Correct 59 ms 60944 KB Output is correct
70 Correct 63 ms 61168 KB Output is correct
71 Correct 66 ms 61324 KB Output is correct
72 Correct 72 ms 61880 KB Output is correct
73 Correct 56 ms 61064 KB Output is correct
74 Correct 223 ms 89892 KB Output is correct
75 Correct 221 ms 82152 KB Output is correct
76 Correct 539 ms 122192 KB Output is correct
77 Correct 586 ms 122272 KB Output is correct
78 Correct 392 ms 116920 KB Output is correct
79 Correct 414 ms 116992 KB Output is correct
80 Correct 175 ms 69572 KB Output is correct
81 Correct 290 ms 116800 KB Output is correct
82 Correct 284 ms 116812 KB Output is correct
83 Correct 340 ms 107880 KB Output is correct
84 Correct 356 ms 108976 KB Output is correct
85 Correct 354 ms 114460 KB Output is correct
86 Correct 296 ms 116820 KB Output is correct
87 Correct 355 ms 125372 KB Output is correct
88 Correct 406 ms 117552 KB Output is correct
89 Correct 321 ms 108580 KB Output is correct
90 Correct 277 ms 117952 KB Output is correct
91 Correct 252 ms 116164 KB Output is correct
92 Correct 249 ms 111032 KB Output is correct