답안 #415518

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
415518 2021-06-01T07:56:27 Z SeDunion 고대 책들 (IOI17_books) C++17
42 / 100
2000 ms 19860 KB
#include "books.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
 
void extend(int &l, int &r, vector<int>&C, vector<int>&L, vector<int>&R) {
	int ll = l, rr = r;
  	for (int i = l ; i <= min(r, l + 10) ; ++ i) ll = min(ll, L[C[i]]), rr = max(rr, R[C[i]]);
 	for (int i = max(l, i - 10) ; i <= r ; ++ i) ll = min(ll, L[C[i]]), rr = max(rr, R[C[i]]);
	while (ll != l || rr != r) {
		while (ll < l) {
			--l;
			ll = min(ll, L[C[l]]);
			rr = max(rr, R[C[l]]);
		}
		while (rr > r) {
			++r;
			ll = min(ll, L[C[r]]);
			rr = max(rr, R[C[r]]);
		}
	}
}
 
ll solve(int l, int r, int needl, int needr, vector<int>&C, vector<int>&L, vector<int>&R) {
	extend(l, r, C, L, R);
	if (needl == l && r == needr) return 0ll;
	int l_l = l, r_l = r; ll c_l = 0;
	while (true) {
		if (l_l <= needl) break;
		if (r_l > r) break;
		c_l += 2;
		--l_l;
		extend(l_l, r_l, C, L, R);
	}
	int l_r = l, r_r = r; ll c_r = 0;
	while (true) {
		if (r_r >= needr) break;
		if (l_r < l) break;
		c_r += 2;
		++r_r;
		extend(l_r, r_r, C, L, R);
	}
	assert((l_r < l) == (r_l > r));
	ll res = 0;
	if (l_r < l) res = min(c_l, c_r);
	else res = c_l + c_r;
	int tl = min(l_l, l_r);
	int tr = max(r_l, r_r);
	res += solve(tl, tr, needl, needr, C, L, R);
	//cerr << l << " " << r << " " << needl << " " << needr << " " << res << "\n";
	return res;
}
 
ll minimum_walk(vector<int> p, int s) {
	int n = p.size();
	vector<int>C(n, -1), L(n), R(n);
	int l = s, r = s;
	ll essential = 0;
	for (int i = 0 ; i < n ; ++ i) {
		essential += abs(i - p[i]);
		if (C[i] == -1) {
			L[i] = i;
			R[i] = i;
			int j = i;
			do {
				R[i] = max(R[i], j);
				C[j] = i;
				j = p[j];
			} while (i != j);
		}
		if (p[i] != i) l = min(l, i), r = max(r, i);
	}
	return solve(s, s, l, r, C, L, R) + essential;
}

Compilation message

books.cpp: In function 'void extend(int&, int&, std::vector<int>&, std::vector<int>&, std::vector<int>&)':
books.cpp:9:25: warning: 'i' is used uninitialized in this function [-Wuninitialized]
    9 |   for (int i = max(l, i - 10) ; i <= r ; ++ i) ll = min(ll, L[C[i]]), rr = max(rr, R[C[i]]);
      |                       ~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 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 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 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 1 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 1 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 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 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 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 2 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 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 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 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 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 2 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 198 ms 19860 KB Output is correct
31 Correct 190 ms 19820 KB Output is correct
32 Correct 130 ms 19812 KB Output is correct
33 Execution timed out 2090 ms 19812 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 2 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 204 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 2 ms 204 KB Output is correct
18 Correct 2 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 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 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 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 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 2 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 198 ms 19860 KB Output is correct
31 Correct 190 ms 19820 KB Output is correct
32 Correct 130 ms 19812 KB Output is correct
33 Execution timed out 2090 ms 19812 KB Time limit exceeded
34 Halted 0 ms 0 KB -