답안 #624349

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
624349 2022-08-08T02:38:54 Z Temmie 고대 책들 (IOI17_books) C++17
50 / 100
280 ms 77436 KB
#include <bits/stdc++.h>

struct Seg {
	
	int size;
	std::vector <int> v;
	
	Seg(int s) {
		size = 1;
		while (size < s) {
			size <<= 1;
		}
		v.resize(size << 1, -1);
	}
	
	inline void build(const std::vector <int>& a) {
		build(a, 0, 0, size);
	}
	
	void build(const std::vector <int>& a, int now, int l, int r) {
		if (!(r - l - 1)) {
			if (l < (int) a.size()) {
				v[now] = a[l];
			}
			return;
		}
		int mid = (l + r) >> 1;
		build(a, now * 2 + 1, l, mid);
		build(a, now * 2 + 2, mid, r);
		v[now] = std::max(v[now * 2 + 1], v[now * 2 + 2]);
	}
	
	inline int query(int l, int r) {
		return query(l, r, 0, 0, size);
	}
	
	int query(int tl, int tr, int now, int l, int r) {
		if (l >= tr || r <= tl) {
			return -1;
		}
		if (l >= tl && r <= tr) {
			return v[now];
		}
		int mid = (l + r) >> 1;
		return std::max(query(tl, tr, now * 2 + 1, l, mid), query(tl, tr, now * 2 + 2, mid, r));
	}
	
};

struct Cycle {
	std::vector <int> c;
	int l, r;
};

long long minimum_walk(std::vector <int> p, int s) {
	int n = p.size();
	while (p.size() && p[n - 1] == n - 1) {
		p.pop_back();
		n--;
	}
	if (!n) {
		return 0;
	}
	std::vector <bool> vis(n, false);
	std::vector <Cycle> cyc;
	std::vector <int> part(n, -1);
	long long ans = 0;
	for (int i = 0; i < n; i++) {
		if (vis[i]) {
			continue;
		}
		vis[i] = true;
		int part_now = cyc.size();
		cyc.resize(cyc.size() + 1);
		cyc.back().c = { i };
		cyc.back().l = cyc.back().r = i;
		ans += abs(i - p[i]);
		int v = p[i];
		part[i] = part_now;
		while (v != i) {
			vis[v] = true;
			cyc.back().c.push_back(v);
			cyc.back().l = std::min(cyc.back().l, v);
			cyc.back().r = std::max(cyc.back().r, v);
			part[v] = part_now;
			ans += abs(v - p[v]);
			v = p[v];
		};
	}
	Seg seg(n);
	std::vector <int> bu(n);
	for (int i = 0; i < n; i++) {
		bu[i] = cyc[part[i]].r;
	}
	seg.build(bu);
	int cmps = 0;
	int l = 0, r = 0;
	int last;
	while (r < n - 1) {
		while ((last = seg.query(l, r + 1)) > r) {
			r = last;
		}
		l = ++r;
		cmps++;
	}
	return ans + (cmps - 1) * 2;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 300 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 304 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 300 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 304 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 1 ms 300 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 300 KB Output is correct
24 Correct 1 ms 300 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 300 KB Output is correct
27 Correct 1 ms 296 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 300 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 304 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 1 ms 300 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 300 KB Output is correct
24 Correct 1 ms 300 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 300 KB Output is correct
27 Correct 1 ms 296 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 186 ms 36012 KB Output is correct
31 Correct 192 ms 36352 KB Output is correct
32 Correct 106 ms 14820 KB Output is correct
33 Correct 266 ms 74536 KB Output is correct
34 Correct 280 ms 74512 KB Output is correct
35 Correct 266 ms 68664 KB Output is correct
36 Correct 194 ms 49716 KB Output is correct
37 Correct 137 ms 38220 KB Output is correct
38 Correct 142 ms 36944 KB Output is correct
39 Correct 129 ms 36692 KB Output is correct
40 Correct 130 ms 35684 KB Output is correct
41 Correct 145 ms 35532 KB Output is correct
42 Correct 143 ms 35416 KB Output is correct
43 Correct 201 ms 77436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 300 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 304 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 1 ms 300 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 300 KB Output is correct
24 Correct 1 ms 300 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 300 KB Output is correct
27 Correct 1 ms 296 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 186 ms 36012 KB Output is correct
31 Correct 192 ms 36352 KB Output is correct
32 Correct 106 ms 14820 KB Output is correct
33 Correct 266 ms 74536 KB Output is correct
34 Correct 280 ms 74512 KB Output is correct
35 Correct 266 ms 68664 KB Output is correct
36 Correct 194 ms 49716 KB Output is correct
37 Correct 137 ms 38220 KB Output is correct
38 Correct 142 ms 36944 KB Output is correct
39 Correct 129 ms 36692 KB Output is correct
40 Correct 130 ms 35684 KB Output is correct
41 Correct 145 ms 35532 KB Output is correct
42 Correct 143 ms 35416 KB Output is correct
43 Correct 201 ms 77436 KB Output is correct
44 Incorrect 0 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -