Submission #392854

# Submission time Handle Problem Language Result Execution time Memory
392854 2021-04-22T00:51:22 Z faresbasbs Ancient Books (IOI17_books) C++14
100 / 100
206 ms 42232 KB
#include <bits/stdc++.h>
#include "books.h"
using namespace std;
long long n,ans,mini,maxi,num,cyc[1000001],L[1000001],R[1000001];
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){
	long long ret = 0;
	while(l > mini || r < maxi){
		extend(l,r);
		bool nxtl = false;
		long long costl = 0 , ll = l ,rl = r;
		while(true){
			if(ll <= mini){
				break;
			}
			ll -= 1;
			costl += 2;
			extend(ll,rl);
			if(rl > r){
				nxtl = true;
				break;
			}
		}
		bool nxtr = false;
		long long costr = 0 , lr = l , rr = r;
		while(true){
			if(rr >= maxi){
				break;
			}
			rr += 1;
			costr += 2;
			extend(lr,rr);
			if(lr < l){
				nxtr = true;
				break;
			}
		}
		if(nxtl && nxtr){
			ret += min(costl,costr);
		}else{
			ret += costl+costr;
		}
		l = min(ll,lr) , r = max(rl,rr);
	}
	return 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;
	}
	return ans+getans(s,s);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 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 336 KB Output is correct
5 Correct 1 ms 204 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 1 ms 332 KB Output is correct
12 Correct 1 ms 336 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 332 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 336 KB Output is correct
5 Correct 1 ms 204 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 1 ms 332 KB Output is correct
12 Correct 1 ms 336 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 336 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 436 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 384 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 332 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 336 KB Output is correct
5 Correct 1 ms 204 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 1 ms 332 KB Output is correct
12 Correct 1 ms 336 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 336 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 436 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 384 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 206 ms 20864 KB Output is correct
31 Correct 205 ms 20732 KB Output is correct
32 Correct 138 ms 35560 KB Output is correct
33 Correct 161 ms 31616 KB Output is correct
34 Correct 146 ms 31696 KB Output is correct
35 Correct 153 ms 29368 KB Output is correct
36 Correct 138 ms 24844 KB Output is correct
37 Correct 132 ms 21372 KB Output is correct
38 Correct 134 ms 20932 KB Output is correct
39 Correct 135 ms 20848 KB Output is correct
40 Correct 135 ms 20852 KB Output is correct
41 Correct 164 ms 20852 KB Output is correct
42 Correct 151 ms 20756 KB Output is correct
43 Correct 142 ms 32388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 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 332 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 332 KB Output is correct
10 Correct 1 ms 332 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 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 336 KB Output is correct
5 Correct 1 ms 204 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 1 ms 332 KB Output is correct
12 Correct 1 ms 336 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 336 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 436 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 384 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 206 ms 20864 KB Output is correct
31 Correct 205 ms 20732 KB Output is correct
32 Correct 138 ms 35560 KB Output is correct
33 Correct 161 ms 31616 KB Output is correct
34 Correct 146 ms 31696 KB Output is correct
35 Correct 153 ms 29368 KB Output is correct
36 Correct 138 ms 24844 KB Output is correct
37 Correct 132 ms 21372 KB Output is correct
38 Correct 134 ms 20932 KB Output is correct
39 Correct 135 ms 20848 KB Output is correct
40 Correct 135 ms 20852 KB Output is correct
41 Correct 164 ms 20852 KB Output is correct
42 Correct 151 ms 20756 KB Output is correct
43 Correct 142 ms 32388 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 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 1 ms 332 KB Output is correct
55 Correct 1 ms 332 KB Output is correct
56 Correct 1 ms 332 KB Output is correct
57 Correct 1 ms 332 KB Output is correct
58 Correct 1 ms 336 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 1 ms 332 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 139 ms 32252 KB Output is correct
65 Correct 143 ms 40132 KB Output is correct
66 Correct 143 ms 28008 KB Output is correct
67 Correct 157 ms 27844 KB Output is correct
68 Correct 15 ms 3380 KB Output is correct
69 Correct 14 ms 3076 KB Output is correct
70 Correct 14 ms 3404 KB Output is correct
71 Correct 15 ms 3900 KB Output is correct
72 Correct 14 ms 4232 KB Output is correct
73 Correct 14 ms 2892 KB Output is correct
74 Correct 149 ms 38392 KB Output is correct
75 Correct 145 ms 38388 KB Output is correct
76 Correct 145 ms 38980 KB Output is correct
77 Correct 144 ms 39024 KB Output is correct
78 Correct 146 ms 37492 KB Output is correct
79 Correct 149 ms 37444 KB Output is correct
80 Correct 132 ms 42232 KB Output is correct
81 Correct 150 ms 35436 KB Output is correct
82 Correct 153 ms 35296 KB Output is correct
83 Correct 173 ms 33516 KB Output is correct
84 Correct 148 ms 32240 KB Output is correct
85 Correct 144 ms 29676 KB Output is correct
86 Correct 138 ms 28404 KB Output is correct
87 Correct 154 ms 40860 KB Output is correct
88 Correct 154 ms 38564 KB Output is correct
89 Correct 150 ms 34700 KB Output is correct
90 Correct 136 ms 27888 KB Output is correct
91 Correct 142 ms 27636 KB Output is correct
92 Correct 146 ms 27532 KB Output is correct