Submission #392851

# Submission time Handle Problem Language Result Execution time Memory
392851 2021-04-22T00:34:23 Z faresbasbs Ancient Books (IOI17_books) C++14
70 / 100
2000 ms 128488 KB
#include <bits/stdc++.h>
#include "books.h"
using namespace std;
long long n,ans,mini,maxi,num,cyc[1000001],L[1000001],R[1000001];
map<pair<int,int>,long long> mp;
bool seen[1000001];
vector<int> p;

void dfs(long long curr){
	seen[curr] = 1 , L[num] = min(L[num],curr) , R[num] = max(R[num],curr) , cyc[curr] = num;
	mini = min(mini,curr) , maxi = max(maxi,curr);
	if(!seen[p[curr]]){
		dfs(p[curr]);
	}
}

void extend(long long &l , long long &r){
	long long ll = min({l,L[cyc[l]],L[cyc[r]]}) , rr = max({r,R[cyc[l]],R[cyc[r]]});
	while(ll < l || r < rr){
		if(ll < l){
			l -= 1;
			ll = min(ll,L[cyc[l]]);
			rr = max(rr,R[cyc[l]]);
		}else{
			r += 1;
			ll = min(ll,L[cyc[r]]);
			rr = max(rr,R[cyc[r]]);
		}
	}
}

long long getans(long long l , long long r){
	extend(l,r);
	if(mp.find({l,r}) != mp.end()){
		return mp[{l,r}];
	}
	long long nl , nr , ret = 1000000000000000000;
	if(l){
		nl = l-1 , nr = r;
		ret = min(ret,2+getans(nl,nr));
	}
	if(r+1 < n){
		nl = l , nr = r+1;
		ret = min(ret,2+getans(nl,nr));
	}
	return mp[{l,r}] = ret;
}

long long minimum_walk(vector<int> P , int s){
	p = P , n = p.size() , mini = maxi = s;
	for(long long i = 0 ; i < n ; i += 1){
		ans += abs(p[i]-i);
		if(seen[i]){
			continue;
		}
		L[num] = R[num] = i , cyc[i] = num;
		if(i != p[i]){
			dfs(i);
		}
		num += 1;
	}
	mp[{mini,maxi}] = 0;
	return ans+getans(s,s);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 316 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 316 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 224 ms 27516 KB Output is correct
31 Correct 217 ms 27500 KB Output is correct
32 Correct 161 ms 42228 KB Output is correct
33 Correct 381 ms 128488 KB Output is correct
34 Correct 368 ms 128488 KB Output is correct
35 Correct 310 ms 100336 KB Output is correct
36 Correct 197 ms 50372 KB Output is correct
37 Correct 150 ms 29044 KB Output is correct
38 Correct 152 ms 27776 KB Output is correct
39 Correct 153 ms 27764 KB Output is correct
40 Correct 153 ms 27512 KB Output is correct
41 Correct 178 ms 27588 KB Output is correct
42 Correct 166 ms 27492 KB Output is correct
43 Correct 156 ms 39148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 6980 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 14 ms 2764 KB Output is correct
4 Correct 38 ms 6584 KB Output is correct
5 Correct 37 ms 6728 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 3 ms 824 KB Output is correct
12 Correct 3 ms 716 KB Output is correct
13 Correct 1 ms 312 KB Output is correct
14 Correct 1 ms 264 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 316 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 224 ms 27516 KB Output is correct
31 Correct 217 ms 27500 KB Output is correct
32 Correct 161 ms 42228 KB Output is correct
33 Correct 381 ms 128488 KB Output is correct
34 Correct 368 ms 128488 KB Output is correct
35 Correct 310 ms 100336 KB Output is correct
36 Correct 197 ms 50372 KB Output is correct
37 Correct 150 ms 29044 KB Output is correct
38 Correct 152 ms 27776 KB Output is correct
39 Correct 153 ms 27764 KB Output is correct
40 Correct 153 ms 27512 KB Output is correct
41 Correct 178 ms 27588 KB Output is correct
42 Correct 166 ms 27492 KB Output is correct
43 Correct 156 ms 39148 KB Output is correct
44 Correct 39 ms 6980 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 14 ms 2764 KB Output is correct
47 Correct 38 ms 6584 KB Output is correct
48 Correct 37 ms 6728 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 1 ms 332 KB Output is correct
53 Correct 1 ms 332 KB Output is correct
54 Correct 3 ms 824 KB Output is correct
55 Correct 3 ms 716 KB Output is correct
56 Correct 1 ms 312 KB Output is correct
57 Correct 1 ms 264 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 1 ms 460 KB Output is correct
61 Correct 1 ms 460 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 312 KB Output is correct
64 Execution timed out 2011 ms 39292 KB Time limit exceeded
65 Halted 0 ms 0 KB -