답안 #249914

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
249914 2020-07-16T12:25:46 Z kostia244 고대 책들 (IOI17_books) C++17
100 / 100
289 ms 82816 KB
#include "books.h"
#include<bits/extc++.h>
using namespace std;
const int maxn = 1<<20;
using ll = long long;
#define end orz
int vis[maxn], a[maxn], col[maxn], n, id = 0;
vector<int> cycle[maxn];
ll ans = 0;
vector<array<int, 2>> segs;
void touch(int p) {
	if(vis[p]) return;
	++id;
	int l = p, r = p, lst = p;
	while(!vis[p]) {
		vis[p] = 1;
		l = min(l, p);
		r = max(r, p);
		
		col[p] = id;
		
		lst = p;
		p = a[p];
		ans += abs(p-lst);
	}
}
int L, R, pL, pR;
void upd(int i) {
	pL = min(pL, cycle[col[i]][0]);
	pR = max(pR, cycle[col[i]].back());
}
void expand() {
	while(pL < L || pR > R) {
		while(L > pL) upd(--L);
		while(R < pR) upd(++R);
	}
}
int start = -1, end = -1;
template<int fl>
pair<int, int> find() {
	int bl = L, br = R, cost = 0;
	//cout << fl << "::\n";
	//cout << cost << " " << L << " " << R << " TO " << endl;
	while((!fl ? L == bl : R == br) && (fl ? L>start : R!=end)) {
		fl ? --pL : ++pR;
		++cost;
		expand();
	}
	//cout << cost << " " << L << " " << R << endl;
	int nl = L, nr = R;
	pL = L = bl, R = pR = br;
	if(!fl ? L == nl : R == nr) return {1<<30, -1};
	return {cost, fl ? nl : nr};
}
ll minimum_walk(vector<int> _a, int s) {
	n = _a.size();
	for(int i = 0; i < n; i++) a[i] = _a[i];
	for(int i = 0; i < n; i++) {
		if(a[i] != i || i == s) {
			if(start == -1) start = i;
			end = i;
		}
		touch(i);
	}
	for(int i = 0; i < n; i++) cycle[col[i]].push_back(i);
	memset(vis, 0, sizeof vis);
	pL = pR = R = s, L = s+1;
	expand();
	//cout << start << " " << end << " " << L << " " << R << '\n';
	int mode = 1;
	while(pL != start || pR != end) {
		//cout << start << " " << end << " " << pL << " " << pR << endl;
		if(mode == 2) {
			if(pR != end) {
				pR++;
				ans += 2;
				expand();
				continue;
			}
			if(pL != start) {
				pL--;
				ans += 2;
				expand();
			}
			continue;
		}
		auto [lcst, lp] = find<1>();
		auto [rcst, rp] = find<0>();
		//cout << lp << " " << rp << endl;
		if(lp == -1 && rp == -1) {mode = 2; continue;}
		if(lcst < rcst) pL = lp;
		else pR = rp;
		ans += 2*min(lcst, rcst);
		expand();
	}
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 29056 KB Output is correct
2 Correct 15 ms 29056 KB Output is correct
3 Correct 15 ms 29056 KB Output is correct
4 Correct 15 ms 29116 KB Output is correct
5 Correct 16 ms 29056 KB Output is correct
6 Correct 15 ms 29056 KB Output is correct
7 Correct 22 ms 29056 KB Output is correct
8 Correct 16 ms 29056 KB Output is correct
9 Correct 21 ms 29056 KB Output is correct
10 Correct 15 ms 29056 KB Output is correct
11 Correct 20 ms 29048 KB Output is correct
12 Correct 15 ms 29056 KB Output is correct
13 Correct 15 ms 29056 KB Output is correct
14 Correct 16 ms 29096 KB Output is correct
15 Correct 16 ms 29056 KB Output is correct
16 Correct 15 ms 29056 KB Output is correct
17 Correct 15 ms 29012 KB Output is correct
18 Correct 15 ms 29184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 29056 KB Output is correct
2 Correct 15 ms 29056 KB Output is correct
3 Correct 15 ms 29056 KB Output is correct
4 Correct 15 ms 29116 KB Output is correct
5 Correct 16 ms 29056 KB Output is correct
6 Correct 15 ms 29056 KB Output is correct
7 Correct 22 ms 29056 KB Output is correct
8 Correct 16 ms 29056 KB Output is correct
9 Correct 21 ms 29056 KB Output is correct
10 Correct 15 ms 29056 KB Output is correct
11 Correct 20 ms 29048 KB Output is correct
12 Correct 15 ms 29056 KB Output is correct
13 Correct 15 ms 29056 KB Output is correct
14 Correct 16 ms 29096 KB Output is correct
15 Correct 16 ms 29056 KB Output is correct
16 Correct 15 ms 29056 KB Output is correct
17 Correct 15 ms 29012 KB Output is correct
18 Correct 15 ms 29184 KB Output is correct
19 Correct 15 ms 29056 KB Output is correct
20 Correct 16 ms 29056 KB Output is correct
21 Correct 16 ms 29056 KB Output is correct
22 Correct 15 ms 29056 KB Output is correct
23 Correct 16 ms 29056 KB Output is correct
24 Correct 16 ms 29056 KB Output is correct
25 Correct 15 ms 29056 KB Output is correct
26 Correct 16 ms 29056 KB Output is correct
27 Correct 16 ms 29056 KB Output is correct
28 Correct 15 ms 29056 KB Output is correct
29 Correct 16 ms 29056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 29056 KB Output is correct
2 Correct 15 ms 29056 KB Output is correct
3 Correct 15 ms 29056 KB Output is correct
4 Correct 15 ms 29116 KB Output is correct
5 Correct 16 ms 29056 KB Output is correct
6 Correct 15 ms 29056 KB Output is correct
7 Correct 22 ms 29056 KB Output is correct
8 Correct 16 ms 29056 KB Output is correct
9 Correct 21 ms 29056 KB Output is correct
10 Correct 15 ms 29056 KB Output is correct
11 Correct 20 ms 29048 KB Output is correct
12 Correct 15 ms 29056 KB Output is correct
13 Correct 15 ms 29056 KB Output is correct
14 Correct 16 ms 29096 KB Output is correct
15 Correct 16 ms 29056 KB Output is correct
16 Correct 15 ms 29056 KB Output is correct
17 Correct 15 ms 29012 KB Output is correct
18 Correct 15 ms 29184 KB Output is correct
19 Correct 15 ms 29056 KB Output is correct
20 Correct 16 ms 29056 KB Output is correct
21 Correct 16 ms 29056 KB Output is correct
22 Correct 15 ms 29056 KB Output is correct
23 Correct 16 ms 29056 KB Output is correct
24 Correct 16 ms 29056 KB Output is correct
25 Correct 15 ms 29056 KB Output is correct
26 Correct 16 ms 29056 KB Output is correct
27 Correct 16 ms 29056 KB Output is correct
28 Correct 15 ms 29056 KB Output is correct
29 Correct 16 ms 29056 KB Output is correct
30 Correct 286 ms 50276 KB Output is correct
31 Correct 289 ms 48980 KB Output is correct
32 Correct 230 ms 76024 KB Output is correct
33 Correct 239 ms 66552 KB Output is correct
34 Correct 236 ms 66552 KB Output is correct
35 Correct 242 ms 62456 KB Output is correct
36 Correct 223 ms 55288 KB Output is correct
37 Correct 203 ms 50936 KB Output is correct
38 Correct 201 ms 50424 KB Output is correct
39 Correct 201 ms 50300 KB Output is correct
40 Correct 202 ms 49396 KB Output is correct
41 Correct 226 ms 49384 KB Output is correct
42 Correct 217 ms 49528 KB Output is correct
43 Correct 233 ms 67960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 29056 KB Output is correct
2 Correct 15 ms 29184 KB Output is correct
3 Correct 16 ms 29056 KB Output is correct
4 Correct 16 ms 29056 KB Output is correct
5 Correct 16 ms 29056 KB Output is correct
6 Correct 16 ms 29056 KB Output is correct
7 Correct 16 ms 29056 KB Output is correct
8 Correct 15 ms 29056 KB Output is correct
9 Correct 16 ms 29056 KB Output is correct
10 Correct 17 ms 29056 KB Output is correct
11 Correct 16 ms 29056 KB Output is correct
12 Correct 16 ms 29056 KB Output is correct
13 Correct 16 ms 29184 KB Output is correct
14 Correct 16 ms 29056 KB Output is correct
15 Correct 15 ms 29056 KB Output is correct
16 Correct 15 ms 29068 KB Output is correct
17 Correct 16 ms 29184 KB Output is correct
18 Correct 16 ms 29056 KB Output is correct
19 Correct 16 ms 29048 KB Output is correct
20 Correct 16 ms 29056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 29056 KB Output is correct
2 Correct 15 ms 29056 KB Output is correct
3 Correct 15 ms 29056 KB Output is correct
4 Correct 15 ms 29116 KB Output is correct
5 Correct 16 ms 29056 KB Output is correct
6 Correct 15 ms 29056 KB Output is correct
7 Correct 22 ms 29056 KB Output is correct
8 Correct 16 ms 29056 KB Output is correct
9 Correct 21 ms 29056 KB Output is correct
10 Correct 15 ms 29056 KB Output is correct
11 Correct 20 ms 29048 KB Output is correct
12 Correct 15 ms 29056 KB Output is correct
13 Correct 15 ms 29056 KB Output is correct
14 Correct 16 ms 29096 KB Output is correct
15 Correct 16 ms 29056 KB Output is correct
16 Correct 15 ms 29056 KB Output is correct
17 Correct 15 ms 29012 KB Output is correct
18 Correct 15 ms 29184 KB Output is correct
19 Correct 15 ms 29056 KB Output is correct
20 Correct 16 ms 29056 KB Output is correct
21 Correct 16 ms 29056 KB Output is correct
22 Correct 15 ms 29056 KB Output is correct
23 Correct 16 ms 29056 KB Output is correct
24 Correct 16 ms 29056 KB Output is correct
25 Correct 15 ms 29056 KB Output is correct
26 Correct 16 ms 29056 KB Output is correct
27 Correct 16 ms 29056 KB Output is correct
28 Correct 15 ms 29056 KB Output is correct
29 Correct 16 ms 29056 KB Output is correct
30 Correct 286 ms 50276 KB Output is correct
31 Correct 289 ms 48980 KB Output is correct
32 Correct 230 ms 76024 KB Output is correct
33 Correct 239 ms 66552 KB Output is correct
34 Correct 236 ms 66552 KB Output is correct
35 Correct 242 ms 62456 KB Output is correct
36 Correct 223 ms 55288 KB Output is correct
37 Correct 203 ms 50936 KB Output is correct
38 Correct 201 ms 50424 KB Output is correct
39 Correct 201 ms 50300 KB Output is correct
40 Correct 202 ms 49396 KB Output is correct
41 Correct 226 ms 49384 KB Output is correct
42 Correct 217 ms 49528 KB Output is correct
43 Correct 233 ms 67960 KB Output is correct
44 Correct 16 ms 29056 KB Output is correct
45 Correct 15 ms 29184 KB Output is correct
46 Correct 16 ms 29056 KB Output is correct
47 Correct 16 ms 29056 KB Output is correct
48 Correct 16 ms 29056 KB Output is correct
49 Correct 16 ms 29056 KB Output is correct
50 Correct 16 ms 29056 KB Output is correct
51 Correct 15 ms 29056 KB Output is correct
52 Correct 16 ms 29056 KB Output is correct
53 Correct 17 ms 29056 KB Output is correct
54 Correct 16 ms 29056 KB Output is correct
55 Correct 16 ms 29056 KB Output is correct
56 Correct 16 ms 29184 KB Output is correct
57 Correct 16 ms 29056 KB Output is correct
58 Correct 15 ms 29056 KB Output is correct
59 Correct 15 ms 29068 KB Output is correct
60 Correct 16 ms 29184 KB Output is correct
61 Correct 16 ms 29056 KB Output is correct
62 Correct 16 ms 29048 KB Output is correct
63 Correct 16 ms 29056 KB Output is correct
64 Correct 244 ms 74844 KB Output is correct
65 Correct 254 ms 76920 KB Output is correct
66 Correct 214 ms 57848 KB Output is correct
67 Correct 223 ms 57336 KB Output is correct
68 Correct 36 ms 32376 KB Output is correct
69 Correct 35 ms 31992 KB Output is correct
70 Correct 40 ms 32632 KB Output is correct
71 Correct 36 ms 33400 KB Output is correct
72 Correct 38 ms 33912 KB Output is correct
73 Correct 34 ms 31736 KB Output is correct
74 Correct 246 ms 73336 KB Output is correct
75 Correct 243 ms 73336 KB Output is correct
76 Correct 250 ms 74360 KB Output is correct
77 Correct 258 ms 74616 KB Output is correct
78 Correct 242 ms 71288 KB Output is correct
79 Correct 242 ms 71288 KB Output is correct
80 Correct 224 ms 82816 KB Output is correct
81 Correct 225 ms 67064 KB Output is correct
82 Correct 224 ms 67064 KB Output is correct
83 Correct 239 ms 64376 KB Output is correct
84 Correct 233 ms 62840 KB Output is correct
85 Correct 217 ms 59384 KB Output is correct
86 Correct 220 ms 58104 KB Output is correct
87 Correct 255 ms 77944 KB Output is correct
88 Correct 248 ms 73464 KB Output is correct
89 Correct 256 ms 66424 KB Output is correct
90 Correct 218 ms 57592 KB Output is correct
91 Correct 210 ms 56952 KB Output is correct
92 Correct 209 ms 56440 KB Output is correct