Submission #355481

# Submission time Handle Problem Language Result Execution time Memory
355481 2021-01-22T14:27:59 Z amunduzbaev Ancient Books (IOI17_books) C++14
50 / 100
379 ms 132588 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 = 0, rans = 0;
	
	// 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;
}
# Verdict Execution time Memory 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 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 492 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 384 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory 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 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 492 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 384 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 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 384 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
# Verdict Execution time Memory 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 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 492 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 384 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 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 384 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 226 ms 15980 KB Output is correct
31 Correct 224 ms 17004 KB Output is correct
32 Correct 142 ms 24812 KB Output is correct
33 Correct 379 ms 132580 KB Output is correct
34 Correct 369 ms 132588 KB Output is correct
35 Correct 309 ms 99824 KB Output is correct
36 Correct 189 ms 42084 KB Output is correct
37 Correct 146 ms 18504 KB Output is correct
38 Correct 148 ms 17260 KB Output is correct
39 Correct 146 ms 17260 KB Output is correct
40 Correct 153 ms 17004 KB Output is correct
41 Correct 179 ms 17192 KB Output is correct
42 Correct 155 ms 17004 KB Output is correct
43 Correct 148 ms 22764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 492 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3302'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 492 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 384 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 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 384 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 226 ms 15980 KB Output is correct
31 Correct 224 ms 17004 KB Output is correct
32 Correct 142 ms 24812 KB Output is correct
33 Correct 379 ms 132580 KB Output is correct
34 Correct 369 ms 132588 KB Output is correct
35 Correct 309 ms 99824 KB Output is correct
36 Correct 189 ms 42084 KB Output is correct
37 Correct 146 ms 18504 KB Output is correct
38 Correct 148 ms 17260 KB Output is correct
39 Correct 146 ms 17260 KB Output is correct
40 Correct 153 ms 17004 KB Output is correct
41 Correct 179 ms 17192 KB Output is correct
42 Correct 155 ms 17004 KB Output is correct
43 Correct 148 ms 22764 KB Output is correct
44 Incorrect 1 ms 492 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3302'
45 Halted 0 ms 0 KB -