답안 #768558

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
768558 2023-06-28T09:05:30 Z SanguineChameleon 고대 책들 (IOI17_books) C++17
100 / 100
157 ms 22672 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

const int maxN = 1e6 + 20;
int cycleL[maxN];
int cycleR[maxN];

void extend(int &curL, int &curR) {
	int nxtL = min(curL, min(cycleL[curL], cycleL[curR]));
	int nxtR = max(curR, max(cycleR[curL], cycleR[curR]));
	while (curL > nxtL || curR < nxtR) {
		if (curL > nxtL) {
			curL--;
			nxtL = min(nxtL, cycleL[curL]);
			nxtR = max(nxtR, cycleR[curL]);
		}
		else {
			curR++;
			nxtL = min(nxtL, cycleL[curR]);
			nxtR = max(nxtR, cycleR[curR]);
		}
	}
}

long long minimum_walk(vector<int> A, int start) {
	int N = A.size();
	for (int i = 0; i < N; i++) {
		cycleL[i] = -1;
		cycleR[i] = -1;
	}
	int curL = start;
	int curR = start;
	int endL = start;
	int endR = start;
	long long res = 0;
	for (int i = 0; i < N; i++) {
		if (i != A[i]) {
			endL = min(endL, i);
			endR = max(endR, i);
		}
		res += abs(A[i] - i);
		if (cycleL[i] == -1) {
			int L = i;
			int R = i;
			int cur = i;
			do {
				L = min(L, cur);
				R = max(R, cur);
				cur = A[cur];
			}
			while (cur != i);
			cur = i;
			do {
				cycleL[cur] = L;
				cycleR[cur] = R;
				cur = A[cur];
			}
			while (cur != i);
		}
	}
	while (curL > endL || curR < endR) {
		extend(curL, curR);
		int costL = 0;
		int nxtLL = curL;
		int nxtLR = curR;
		while (nxtLL > endL && nxtLR == curR) {
			nxtLL--;
			costL += 2;
			extend(nxtLL, nxtLR);
		}
		int costR = 0;
		int nxtRL = curL;
		int nxtRR = curR;
		while (nxtRR < endR && nxtRL == curL) {
			nxtRR++;
			costR += 2;
			extend(nxtRL, nxtRR);
		}
		if (nxtLR == curR && nxtRL == curL) {
			res += costL + costR;
		}
		else {
			res += min(costL, costR);
		}
		curL = min(nxtLL, nxtRL);
		curR = max(nxtLR, nxtRR);
	}
	return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 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 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 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 1 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 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 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 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 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 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 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 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 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 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 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 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 119 ms 15948 KB Output is correct
31 Correct 157 ms 15872 KB Output is correct
32 Correct 75 ms 15860 KB Output is correct
33 Correct 85 ms 15864 KB Output is correct
34 Correct 100 ms 15820 KB Output is correct
35 Correct 92 ms 15860 KB Output is correct
36 Correct 83 ms 15872 KB Output is correct
37 Correct 89 ms 15876 KB Output is correct
38 Correct 81 ms 15868 KB Output is correct
39 Correct 84 ms 15872 KB Output is correct
40 Correct 92 ms 15868 KB Output is correct
41 Correct 111 ms 15868 KB Output is correct
42 Correct 105 ms 15940 KB Output is correct
43 Correct 81 ms 15868 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 1 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 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 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 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 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 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 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 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 119 ms 15948 KB Output is correct
31 Correct 157 ms 15872 KB Output is correct
32 Correct 75 ms 15860 KB Output is correct
33 Correct 85 ms 15864 KB Output is correct
34 Correct 100 ms 15820 KB Output is correct
35 Correct 92 ms 15860 KB Output is correct
36 Correct 83 ms 15872 KB Output is correct
37 Correct 89 ms 15876 KB Output is correct
38 Correct 81 ms 15868 KB Output is correct
39 Correct 84 ms 15872 KB Output is correct
40 Correct 92 ms 15868 KB Output is correct
41 Correct 111 ms 15868 KB Output is correct
42 Correct 105 ms 15940 KB Output is correct
43 Correct 81 ms 15868 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 1 ms 212 KB Output is correct
57 Correct 0 ms 212 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 0 ms 212 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 85 ms 22672 KB Output is correct
65 Correct 83 ms 22628 KB Output is correct
66 Correct 84 ms 22628 KB Output is correct
67 Correct 83 ms 22624 KB Output is correct
68 Correct 9 ms 2352 KB Output is correct
69 Correct 9 ms 2388 KB Output is correct
70 Correct 12 ms 2388 KB Output is correct
71 Correct 10 ms 2388 KB Output is correct
72 Correct 8 ms 2388 KB Output is correct
73 Correct 9 ms 2476 KB Output is correct
74 Correct 86 ms 22624 KB Output is correct
75 Correct 87 ms 22588 KB Output is correct
76 Correct 85 ms 22672 KB Output is correct
77 Correct 85 ms 22640 KB Output is correct
78 Correct 93 ms 22636 KB Output is correct
79 Correct 85 ms 22668 KB Output is correct
80 Correct 75 ms 22632 KB Output is correct
81 Correct 83 ms 22624 KB Output is correct
82 Correct 91 ms 22624 KB Output is correct
83 Correct 86 ms 22624 KB Output is correct
84 Correct 85 ms 22604 KB Output is correct
85 Correct 83 ms 22644 KB Output is correct
86 Correct 90 ms 22672 KB Output is correct
87 Correct 91 ms 22640 KB Output is correct
88 Correct 87 ms 22636 KB Output is correct
89 Correct 90 ms 22640 KB Output is correct
90 Correct 83 ms 22644 KB Output is correct
91 Correct 95 ms 22672 KB Output is correct
92 Correct 93 ms 22624 KB Output is correct