Submission #410421

# Submission time Handle Problem Language Result Execution time Memory
410421 2021-05-22T16:46:28 Z Antekb Ancient Books (IOI17_books) C++14
100 / 100
392 ms 31048 KB
#include "books.h"
#include<bits/stdc++.h>
using namespace std;
const int N=(1<<20);
int tab[N+N];
const int INF=1e9;
void ust(int l, int r, int m){
	r++;
	for(l+=N, r+=N; l<r; l>>=1, r>>=1){
		if(l&1){
			tab[l]=min(tab[l], m);
			l++;
		}
		if(r&1){
			--r;
			tab[r]=min(tab[r], m);
		}
	}
}
int quer(int id){
	int ans=INF;
	for(id+=N; id>0; id>>=1){
		ans=min(ans, tab[id]);
	}
	return ans;
}
long long minimum_walk(std::vector<int> p, int s) {
	int n=p.size();
	/*s=n-s-1;
	for(int i=0; i<n; i++){
		p[i]=n-p[i]-1;
	}
	reverse(p.begin(), p.end());*/
	long long ans=0;
	for(int i=0; i<n; i++){
		ans+=abs(i-p[i]);
	}
	vector<int> V(n), col(n);
	int sum=0;
	for(int i=0; i<n; i++){
		sum-=V[i];
		sum-=V[p[i]];
		V[i]^=1;
		V[p[i]]^=1;
		sum+=V[i];
		sum+=V[p[i]];
		if(sum)col[i]=1;
	}
	bool czy=0;
	for(int i=0; i<s; i++){
		if(col[i])czy=1;
		if(czy && !col[i])ans+=2;
	}
	czy=0;
	for(int i=n-1; i>=s; i--){
		if(col[i])czy=1;
		if(czy && !col[i])ans+=2;
	}
	for(int i=1; i<N+N; i++){
		tab[i]=INF;
	}
	ust(s, s, 0);
	int l=s, r=s;
	while(l>=0 || r<n){
		if(l<0 || quer(l)>quer(r)){
			if(r<n-1)ust(r+1, r+1, quer(r)+1);
			ust(min(r, p[r]), max(r, p[r]), quer(r));
			r++;
		}
		else{
			if(l)ust(l-1, l-1, quer(l)+1);
			ust(min(l, p[l]), max(l, p[l]), quer(l));
			l--;
		}
	}
	int k=0;
	for(int i=0; i<n; i++){
		if((s-i)*(long long)(s-p[i])<0){
			k=max(k, quer(i));
		}
	}
	ans+=2*k;
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8396 KB Output is correct
2 Correct 6 ms 8396 KB Output is correct
3 Correct 6 ms 8396 KB Output is correct
4 Correct 6 ms 8396 KB Output is correct
5 Correct 6 ms 8396 KB Output is correct
6 Correct 6 ms 8380 KB Output is correct
7 Correct 6 ms 8396 KB Output is correct
8 Correct 6 ms 8492 KB Output is correct
9 Correct 6 ms 8396 KB Output is correct
10 Correct 7 ms 8492 KB Output is correct
11 Correct 7 ms 8396 KB Output is correct
12 Correct 6 ms 8492 KB Output is correct
13 Correct 6 ms 8488 KB Output is correct
14 Correct 5 ms 8396 KB Output is correct
15 Correct 6 ms 8488 KB Output is correct
16 Correct 6 ms 8396 KB Output is correct
17 Correct 6 ms 8396 KB Output is correct
18 Correct 6 ms 8396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8396 KB Output is correct
2 Correct 6 ms 8396 KB Output is correct
3 Correct 6 ms 8396 KB Output is correct
4 Correct 6 ms 8396 KB Output is correct
5 Correct 6 ms 8396 KB Output is correct
6 Correct 6 ms 8380 KB Output is correct
7 Correct 6 ms 8396 KB Output is correct
8 Correct 6 ms 8492 KB Output is correct
9 Correct 6 ms 8396 KB Output is correct
10 Correct 7 ms 8492 KB Output is correct
11 Correct 7 ms 8396 KB Output is correct
12 Correct 6 ms 8492 KB Output is correct
13 Correct 6 ms 8488 KB Output is correct
14 Correct 5 ms 8396 KB Output is correct
15 Correct 6 ms 8488 KB Output is correct
16 Correct 6 ms 8396 KB Output is correct
17 Correct 6 ms 8396 KB Output is correct
18 Correct 6 ms 8396 KB Output is correct
19 Correct 6 ms 8524 KB Output is correct
20 Correct 6 ms 8396 KB Output is correct
21 Correct 6 ms 8488 KB Output is correct
22 Correct 6 ms 8396 KB Output is correct
23 Correct 7 ms 8464 KB Output is correct
24 Correct 6 ms 8396 KB Output is correct
25 Correct 6 ms 8396 KB Output is correct
26 Correct 6 ms 8396 KB Output is correct
27 Correct 6 ms 8396 KB Output is correct
28 Correct 6 ms 8396 KB Output is correct
29 Correct 6 ms 8396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8396 KB Output is correct
2 Correct 6 ms 8396 KB Output is correct
3 Correct 6 ms 8396 KB Output is correct
4 Correct 6 ms 8396 KB Output is correct
5 Correct 6 ms 8396 KB Output is correct
6 Correct 6 ms 8380 KB Output is correct
7 Correct 6 ms 8396 KB Output is correct
8 Correct 6 ms 8492 KB Output is correct
9 Correct 6 ms 8396 KB Output is correct
10 Correct 7 ms 8492 KB Output is correct
11 Correct 7 ms 8396 KB Output is correct
12 Correct 6 ms 8492 KB Output is correct
13 Correct 6 ms 8488 KB Output is correct
14 Correct 5 ms 8396 KB Output is correct
15 Correct 6 ms 8488 KB Output is correct
16 Correct 6 ms 8396 KB Output is correct
17 Correct 6 ms 8396 KB Output is correct
18 Correct 6 ms 8396 KB Output is correct
19 Correct 6 ms 8524 KB Output is correct
20 Correct 6 ms 8396 KB Output is correct
21 Correct 6 ms 8488 KB Output is correct
22 Correct 6 ms 8396 KB Output is correct
23 Correct 7 ms 8464 KB Output is correct
24 Correct 6 ms 8396 KB Output is correct
25 Correct 6 ms 8396 KB Output is correct
26 Correct 6 ms 8396 KB Output is correct
27 Correct 6 ms 8396 KB Output is correct
28 Correct 6 ms 8396 KB Output is correct
29 Correct 6 ms 8396 KB Output is correct
30 Correct 375 ms 30820 KB Output is correct
31 Correct 392 ms 31012 KB Output is correct
32 Correct 189 ms 30832 KB Output is correct
33 Correct 201 ms 30812 KB Output is correct
34 Correct 215 ms 30844 KB Output is correct
35 Correct 201 ms 30824 KB Output is correct
36 Correct 213 ms 30788 KB Output is correct
37 Correct 246 ms 30952 KB Output is correct
38 Correct 266 ms 30916 KB Output is correct
39 Correct 269 ms 30768 KB Output is correct
40 Correct 322 ms 30936 KB Output is correct
41 Correct 341 ms 30844 KB Output is correct
42 Correct 340 ms 31044 KB Output is correct
43 Correct 209 ms 30788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8396 KB Output is correct
2 Correct 5 ms 8396 KB Output is correct
3 Correct 5 ms 8396 KB Output is correct
4 Correct 6 ms 8492 KB Output is correct
5 Correct 5 ms 8396 KB Output is correct
6 Correct 6 ms 8396 KB Output is correct
7 Correct 6 ms 8396 KB Output is correct
8 Correct 6 ms 8492 KB Output is correct
9 Correct 6 ms 8396 KB Output is correct
10 Correct 6 ms 8396 KB Output is correct
11 Correct 5 ms 8428 KB Output is correct
12 Correct 5 ms 8396 KB Output is correct
13 Correct 6 ms 8396 KB Output is correct
14 Correct 6 ms 8396 KB Output is correct
15 Correct 6 ms 8396 KB Output is correct
16 Correct 7 ms 8496 KB Output is correct
17 Correct 6 ms 8492 KB Output is correct
18 Correct 6 ms 8396 KB Output is correct
19 Correct 6 ms 8396 KB Output is correct
20 Correct 5 ms 8396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8396 KB Output is correct
2 Correct 6 ms 8396 KB Output is correct
3 Correct 6 ms 8396 KB Output is correct
4 Correct 6 ms 8396 KB Output is correct
5 Correct 6 ms 8396 KB Output is correct
6 Correct 6 ms 8380 KB Output is correct
7 Correct 6 ms 8396 KB Output is correct
8 Correct 6 ms 8492 KB Output is correct
9 Correct 6 ms 8396 KB Output is correct
10 Correct 7 ms 8492 KB Output is correct
11 Correct 7 ms 8396 KB Output is correct
12 Correct 6 ms 8492 KB Output is correct
13 Correct 6 ms 8488 KB Output is correct
14 Correct 5 ms 8396 KB Output is correct
15 Correct 6 ms 8488 KB Output is correct
16 Correct 6 ms 8396 KB Output is correct
17 Correct 6 ms 8396 KB Output is correct
18 Correct 6 ms 8396 KB Output is correct
19 Correct 6 ms 8524 KB Output is correct
20 Correct 6 ms 8396 KB Output is correct
21 Correct 6 ms 8488 KB Output is correct
22 Correct 6 ms 8396 KB Output is correct
23 Correct 7 ms 8464 KB Output is correct
24 Correct 6 ms 8396 KB Output is correct
25 Correct 6 ms 8396 KB Output is correct
26 Correct 6 ms 8396 KB Output is correct
27 Correct 6 ms 8396 KB Output is correct
28 Correct 6 ms 8396 KB Output is correct
29 Correct 6 ms 8396 KB Output is correct
30 Correct 375 ms 30820 KB Output is correct
31 Correct 392 ms 31012 KB Output is correct
32 Correct 189 ms 30832 KB Output is correct
33 Correct 201 ms 30812 KB Output is correct
34 Correct 215 ms 30844 KB Output is correct
35 Correct 201 ms 30824 KB Output is correct
36 Correct 213 ms 30788 KB Output is correct
37 Correct 246 ms 30952 KB Output is correct
38 Correct 266 ms 30916 KB Output is correct
39 Correct 269 ms 30768 KB Output is correct
40 Correct 322 ms 30936 KB Output is correct
41 Correct 341 ms 30844 KB Output is correct
42 Correct 340 ms 31044 KB Output is correct
43 Correct 209 ms 30788 KB Output is correct
44 Correct 6 ms 8396 KB Output is correct
45 Correct 5 ms 8396 KB Output is correct
46 Correct 5 ms 8396 KB Output is correct
47 Correct 6 ms 8492 KB Output is correct
48 Correct 5 ms 8396 KB Output is correct
49 Correct 6 ms 8396 KB Output is correct
50 Correct 6 ms 8396 KB Output is correct
51 Correct 6 ms 8492 KB Output is correct
52 Correct 6 ms 8396 KB Output is correct
53 Correct 6 ms 8396 KB Output is correct
54 Correct 5 ms 8428 KB Output is correct
55 Correct 5 ms 8396 KB Output is correct
56 Correct 6 ms 8396 KB Output is correct
57 Correct 6 ms 8396 KB Output is correct
58 Correct 6 ms 8396 KB Output is correct
59 Correct 7 ms 8496 KB Output is correct
60 Correct 6 ms 8492 KB Output is correct
61 Correct 6 ms 8396 KB Output is correct
62 Correct 6 ms 8396 KB Output is correct
63 Correct 5 ms 8396 KB Output is correct
64 Correct 246 ms 30820 KB Output is correct
65 Correct 246 ms 30892 KB Output is correct
66 Correct 283 ms 30832 KB Output is correct
67 Correct 294 ms 30952 KB Output is correct
68 Correct 31 ms 10652 KB Output is correct
69 Correct 31 ms 10664 KB Output is correct
70 Correct 28 ms 10548 KB Output is correct
71 Correct 29 ms 10572 KB Output is correct
72 Correct 25 ms 10628 KB Output is correct
73 Correct 39 ms 10528 KB Output is correct
74 Correct 234 ms 30932 KB Output is correct
75 Correct 253 ms 30836 KB Output is correct
76 Correct 226 ms 30888 KB Output is correct
77 Correct 228 ms 30820 KB Output is correct
78 Correct 238 ms 30896 KB Output is correct
79 Correct 261 ms 30896 KB Output is correct
80 Correct 210 ms 30896 KB Output is correct
81 Correct 285 ms 30828 KB Output is correct
82 Correct 284 ms 30892 KB Output is correct
83 Correct 245 ms 30832 KB Output is correct
84 Correct 272 ms 30888 KB Output is correct
85 Correct 264 ms 31048 KB Output is correct
86 Correct 306 ms 30788 KB Output is correct
87 Correct 264 ms 30892 KB Output is correct
88 Correct 266 ms 30892 KB Output is correct
89 Correct 247 ms 30824 KB Output is correct
90 Correct 289 ms 30788 KB Output is correct
91 Correct 295 ms 30812 KB Output is correct
92 Correct 332 ms 30804 KB Output is correct