답안 #355508

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
355508 2021-01-22T15:02:06 Z amunduzbaev 고대 책들 (IOI17_books) C++14
100 / 100
369 ms 131692 KB
#include "books.h"
 
#include "bits/stdc++.h"
using namespace std;
#ifndef EVAL
#include "grader.cpp"
#endif
 
#define ll long long
#define sz(x) (int)x.size()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
 
const ll inf = 1e9+7;
const int N = 1e6+5;
const int mod = 1e9+7;
 
int n, used[N], last, L[N], R[N], in[N];
int endL, endR;
map<pii, ll> dp;

void ext(int &l, int &r){
	int new_l = min(L[in[l]], L[in[r]]), new_r = max(R[in[r]], R[in[l]]);
	while(1){
		if(new_l < l) new_l = min(L[in[--l]], new_l), new_r = max(R[in[l]], new_r);
		else if(new_r > r) new_l = min(L[in[++r]], new_l), new_r = max(R[in[r]], new_r);
		else break;
	}//return {new_l, new_r};
}

ll fun(int l, int r){
	ext(l, r);
	if(l == endL && r == endR) return 0;
	if(dp.find(make_pair(l, r)) != dp.end()) return dp[make_pair(l, r)];
	if(l == endL) return dp[{l, r}] = fun(l, r+1)+2;
	if(r == endL) return dp[{l, r}] = fun(l-1, r)+2;
	
	
	int l_l = l-1, l_r = r;  ext(l_l, l_r);
	int r_l = l, r_r = r+1;  ext(r_l, r_r);
	int lans = 1, rans = 1;
	
	// trying to move our tmpl while our tmpr is < our extended {l, r+1} range
	
	while(l_l != endL && l_r < r_r){
		lans++, l_l--;
		ext(l_l, l_r);
	}if(l_r < r_r) return dp[{l, r}] = lans*2 + fun(l_l, l_r);
	
	// trying to move our tmpr while our tmpl is > our extended {l-1, r} range
	
	r_r = r+1, r_l = l; ext(r_l, r_r);
	l_l = l-1, l_r = r; ext(l_l, l_r);
	
	while(r_r != endR && r_l > l_l){
		rans++, r_r++;
		ext(r_l, r_r);
	}if(r_l > l_l) return dp[{l, r}] = rans*2 + fun(r_l, r_r);
	
	return dp[{l, r}] = min(lans, rans)*2 + fun(r_l, r_r);
}

ll minimum_walk(vector<int> p, int s) {
	//memset(dp, -1, sizeof dp);
	ll res = 0;
	endL = s, endR = s, n = sz(p);
	
	for(int i=0;i<n;i++){
		if(!used[i]){
			int u = i;
			L[last] = u, R[last] = u;
			do{
				used[u] = 1;
				in[u] = last;
				L[last] = min(L[last], u);
				R[last] = max(R[last], u);
				res += abs(p[u] - u);
				u = p[u];
			}while(u != i);
			if(L[last] != R[last]){
				endL = min(L[last], endL);
				endR = max(R[last], endR);
			}
			last++;
		}
	}
	
	return fun(s, s) + res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 0 ms 364 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 0 ms 364 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 0 ms 364 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 218 ms 16072 KB Output is correct
31 Correct 217 ms 15980 KB Output is correct
32 Correct 140 ms 23788 KB Output is correct
33 Correct 369 ms 131692 KB Output is correct
34 Correct 368 ms 131692 KB Output is correct
35 Correct 314 ms 98852 KB Output is correct
36 Correct 189 ms 40940 KB Output is correct
37 Correct 164 ms 17644 KB Output is correct
38 Correct 142 ms 16384 KB Output is correct
39 Correct 142 ms 16200 KB Output is correct
40 Correct 146 ms 15944 KB Output is correct
41 Correct 164 ms 15944 KB Output is correct
42 Correct 158 ms 15944 KB Output is correct
43 Correct 148 ms 21868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 0 ms 364 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 218 ms 16072 KB Output is correct
31 Correct 217 ms 15980 KB Output is correct
32 Correct 140 ms 23788 KB Output is correct
33 Correct 369 ms 131692 KB Output is correct
34 Correct 368 ms 131692 KB Output is correct
35 Correct 314 ms 98852 KB Output is correct
36 Correct 189 ms 40940 KB Output is correct
37 Correct 164 ms 17644 KB Output is correct
38 Correct 142 ms 16384 KB Output is correct
39 Correct 142 ms 16200 KB Output is correct
40 Correct 146 ms 15944 KB Output is correct
41 Correct 164 ms 15944 KB Output is correct
42 Correct 158 ms 15944 KB Output is correct
43 Correct 148 ms 21868 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 1 ms 364 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 1 ms 364 KB Output is correct
50 Correct 1 ms 364 KB Output is correct
51 Correct 1 ms 364 KB Output is correct
52 Correct 1 ms 364 KB Output is correct
53 Correct 1 ms 364 KB Output is correct
54 Correct 1 ms 364 KB Output is correct
55 Correct 1 ms 364 KB Output is correct
56 Correct 1 ms 364 KB Output is correct
57 Correct 1 ms 364 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 1 ms 384 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 364 KB Output is correct
64 Correct 147 ms 22764 KB Output is correct
65 Correct 145 ms 23276 KB Output is correct
66 Correct 141 ms 17132 KB Output is correct
67 Correct 144 ms 17004 KB Output is correct
68 Correct 15 ms 2668 KB Output is correct
69 Correct 14 ms 2540 KB Output is correct
70 Correct 14 ms 2796 KB Output is correct
71 Correct 15 ms 3052 KB Output is correct
72 Correct 15 ms 3180 KB Output is correct
73 Correct 15 ms 2540 KB Output is correct
74 Correct 232 ms 66284 KB Output is correct
75 Correct 254 ms 77420 KB Output is correct
76 Correct 153 ms 22636 KB Output is correct
77 Correct 156 ms 22644 KB Output is correct
78 Correct 163 ms 21868 KB Output is correct
79 Correct 150 ms 21868 KB Output is correct
80 Correct 139 ms 24684 KB Output is correct
81 Correct 259 ms 78316 KB Output is correct
82 Correct 261 ms 78244 KB Output is correct
83 Correct 160 ms 24940 KB Output is correct
84 Correct 156 ms 23004 KB Output is correct
85 Correct 149 ms 19292 KB Output is correct
86 Correct 143 ms 17664 KB Output is correct
87 Correct 203 ms 50028 KB Output is correct
88 Correct 184 ms 38148 KB Output is correct
89 Correct 177 ms 27500 KB Output is correct
90 Correct 144 ms 17132 KB Output is correct
91 Correct 149 ms 17004 KB Output is correct
92 Correct 154 ms 16876 KB Output is correct