답안 #526877

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
526877 2022-02-16T13:29:54 Z KoD 고대 책들 (IOI17_books) C++17
50 / 100
800 ms 209064 KB
#include <bits/stdc++.h>
#include "books.h"

using std::vector;
using std::array;
using std::pair;
using std::tuple;

using ll = long long;

struct UnionFind {
	vector<int> par;
	UnionFind(const int n) : par(n, -1) {}
	int find(const int u) {
		return par[u] < 0 ? u : par[u] = find(par[u]);
	}
	void merge(int u, int v) {
		u = find(u), v = find(v);
		if (u == v) return;
		if (par[u] > par[v]) std::swap(u, v);
		par[u] += par[v];
		par[v] = u;
		return;
	}
};

ll minimum_walk(vector<int> P, int src) {
	{
		int l = 0;
		while (l < src and P[l] == l) l += 1;
		int r = (int)P.size() - 1;
		while (r > src and P[r] == r) r -= 1;
		P.erase(P.begin() + r + 1, P.end());
		P.erase(P.begin(), P.begin() + l);
		for (auto& x : P) x -= l;
		src -= l;
	}
	const int N = P.size();
	vector<vector<int>> cycle;
	{
		vector<char> done(N);
		for (int i = 0; i < N; ++i) {
			if (done[i]) continue;
			auto& v = cycle.emplace_back();
			for (int j = i; !done[j]; j = P[j]) {
				v.push_back(j);
				done[j] = true;
			}
		}
	}
	const int M = cycle.size();
	UnionFind dsu(M);
	{
		vector<tuple<int, int, int>> query;
		for (int i = 0; i < M; ++i) {
			auto& v = cycle[i];
			std::sort(v.begin(), v.end());
			for (int j = 1; j < (int)v.size(); ++j) {
				query.emplace_back(N + v[j - 1], N + v[j], i);
			}
		}
		std::sort(query.begin(), query.end());
		vector<vector<int>> list(2 * N);
		const auto process = [&](const int k, const int i) {
			for (const int j : list[k]) dsu.merge(i, j);
			list[k] = {i};
		};
		for (const auto& [lc, rc, i] : query) {
			for (int l = lc, r = rc; l < r; l >>= 1, r >>= 1) {
				if (l & 1) process(l++, i);
				if (r & 1) process(--r, i);
			}
			for (int k = rc; k > 0; k >>= 1) list[k].push_back(i);
		}
	}
	vector<int> belongs(N);
	for (int i = 0; i < M; ++i) {
		const int p = dsu.find(i);
		for (const int j : cycle[i]) {
			belongs[j] = p;
		}
	}
	vector<vector<int>> list(M);
	for (int i = 0; i < N; ++i) list[belongs[i]].push_back(i);
	vector<int> parent(M, -1);
	{
		vector<int> start(N, -1), end(N, -1);
		for (int j = 0; j < M; ++j) {
			if (!list[j].empty()) {
				start[list[j].front()] = j;
				end[list[j].back()] = j;
			}
		}
		vector<int> stack;
		for (int i = 0; i < N; ++i) {
			if (start[i] != -1) {
				if (!stack.empty()) parent[start[i]] = stack.back();
				stack.push_back(start[i]);
			}
			if (end[i] != -1) stack.pop_back();
		}
	}
	vector<int> dist(M, N + 1);
	{
		std::queue<int> que;
		const auto push = [&](const int u, const int d) {
			if (dist[u] > d) {
				dist[u] = d;
				que.push(u);
			}
		};
		push(belongs[src], 0);
		while (!que.empty()) {
			const int u = que.front();
			que.pop();
			const int l = list[u].front();
			const int r = list[u].back();
			if (l > 0) push(belongs[l - 1], dist[u] + 1);
			if (r + 1 < N) push(belongs[r + 1], dist[u] + 1);
		}
	}
	int root = belongs[src];
	while (parent[root] != -1) root = parent[root];
	ll ret = 2 * dist[root];
	vector<int> coeff(N);
	for (int i = 0; i < N; ++i) {
		const int l = std::min(i, P[i]);	
		const int r = std::max(i, P[i]);	
		ret += r - l;
		coeff[l] += 1;
		coeff[r] -= 1;
	}
	for (int i = 0; i < N - 1; ++i) {
		if (coeff[i] == 0) ret += 2;
		coeff[i + 1] += coeff[i];
	}
	return ret;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 1 ms 292 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 292 KB Output is correct
13 Correct 0 ms 208 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 1 ms 292 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 292 KB Output is correct
13 Correct 0 ms 208 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 420 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 296 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 412 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 1 ms 292 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 292 KB Output is correct
13 Correct 0 ms 208 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 420 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 296 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 412 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 747 ms 207952 KB Output is correct
31 Correct 800 ms 209064 KB Output is correct
32 Correct 84 ms 8508 KB Output is correct
33 Correct 485 ms 163356 KB Output is correct
34 Correct 485 ms 163492 KB Output is correct
35 Correct 541 ms 173376 KB Output is correct
36 Correct 619 ms 199708 KB Output is correct
37 Correct 672 ms 204308 KB Output is correct
38 Correct 710 ms 205948 KB Output is correct
39 Correct 707 ms 206012 KB Output is correct
40 Correct 723 ms 207308 KB Output is correct
41 Correct 758 ms 207976 KB Output is correct
42 Correct 762 ms 207968 KB Output is correct
43 Correct 545 ms 162532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 400 KB Output is correct
3 Correct 1 ms 416 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Incorrect 1 ms 332 KB 3rd lines differ - on the 1st token, expected: '22140', found: '22136'
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 1 ms 292 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 292 KB Output is correct
13 Correct 0 ms 208 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 420 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 296 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 412 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 747 ms 207952 KB Output is correct
31 Correct 800 ms 209064 KB Output is correct
32 Correct 84 ms 8508 KB Output is correct
33 Correct 485 ms 163356 KB Output is correct
34 Correct 485 ms 163492 KB Output is correct
35 Correct 541 ms 173376 KB Output is correct
36 Correct 619 ms 199708 KB Output is correct
37 Correct 672 ms 204308 KB Output is correct
38 Correct 710 ms 205948 KB Output is correct
39 Correct 707 ms 206012 KB Output is correct
40 Correct 723 ms 207308 KB Output is correct
41 Correct 758 ms 207976 KB Output is correct
42 Correct 762 ms 207968 KB Output is correct
43 Correct 545 ms 162532 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 400 KB Output is correct
46 Correct 1 ms 416 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Incorrect 1 ms 332 KB 3rd lines differ - on the 1st token, expected: '22140', found: '22136'
50 Halted 0 ms 0 KB -