Submission #758030

# Submission time Handle Problem Language Result Execution time Memory
758030 2023-06-14T05:10:28 Z alexander707070 Ancient Books (IOI17_books) C++14
50 / 100
920 ms 24844 KB
#include<bits/stdc++.h>
#define MAXN 1000007
using namespace std;
 
int n,perm[MAXN],br,cnt,last,maxpos,to[MAXN],curr,pos,first;
bool li[MAXN],ok,used[MAXN];
long long sum,add[MAXN],ans;

bool tree[4*MAXN];

void update(int v,int l,int r,int ll,int rr){
	if(ll>rr)return;
	if(l==ll and r==rr){
		tree[v]=true;
	}else{
		int tt=(l+r)/2;
		update(2*v,l,tt,ll,min(tt,rr));
		update(2*v+1,tt+1,r,max(tt+1,ll),rr);
	}
}

bool getpos(int v,int l,int r,int pos){
	if(l==r)return tree[v];
	int tt=(l+r)/2;
	if(pos<=tt)return getpos(2*v,l,tt,pos) or tree[v];
	else return getpos(2*v+1,tt+1,r,pos)  or tree[v];
}
 
void dfs(int x){
	li[x]=true; 
	sum+=abs(x-perm[x]); br++;

	update(1,0,n-1,min(x,perm[x]),max(x,perm[x])-1);

	if(!li[perm[x]])dfs(perm[x]);
}

void dfs2(int x){
	li[x]=true; 
	sum+=abs(x-perm[x]); br++;

	update(1,0,n-1,min(x,perm[x])+1,max(x,perm[x]));

	if(!li[perm[x]])dfs2(perm[x]);
}
 
long long minimum_walk(vector<int> p, int s){
	n=int(p.size());
 
	for(int i=0;i<n;i++)perm[i]=p[i];
 
	for(int i=0;i<n;i++){
		if(!li[i]){
			sum=br=0; 
			dfs(i); add[i]=sum;
			if(br>1)last=i;
		}
	}

	for(int i=s;i<=last;i++){
		ans+=add[i];
		if(!getpos(1,0,n-1,i) and i<last)ans+=2;
	}

	for(int i=0;i<4*n;i++)tree[i]=false;
	for(int i=0;i<n;i++)li[i]=false;

	for(int i=n-1;i>=0;i--){
		if(!li[i]){
			sum=br=0; 
			dfs2(i); add[i]=sum;
			if(br>1)first=i;
		}
	}

	for(int i=s;i>=first;i--){
		ans+=add[i];
		if(!getpos(1,0,n-1,i) and i>first)ans+=2;
	}

	return ans;
}
 
/*
int main(){
	cout<<minimum_walk({0,2,3,1}, 1)<<"\n";
}
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 850 ms 16972 KB Output is correct
31 Correct 920 ms 17016 KB Output is correct
32 Correct 101 ms 24708 KB Output is correct
33 Correct 337 ms 24840 KB Output is correct
34 Correct 325 ms 24844 KB Output is correct
35 Correct 366 ms 24772 KB Output is correct
36 Correct 444 ms 24808 KB Output is correct
37 Correct 533 ms 24736 KB Output is correct
38 Correct 611 ms 23068 KB Output is correct
39 Correct 624 ms 22532 KB Output is correct
40 Correct 692 ms 18936 KB Output is correct
41 Correct 815 ms 17120 KB Output is correct
42 Correct 847 ms 17472 KB Output is correct
43 Correct 333 ms 24780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '1050'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 850 ms 16972 KB Output is correct
31 Correct 920 ms 17016 KB Output is correct
32 Correct 101 ms 24708 KB Output is correct
33 Correct 337 ms 24840 KB Output is correct
34 Correct 325 ms 24844 KB Output is correct
35 Correct 366 ms 24772 KB Output is correct
36 Correct 444 ms 24808 KB Output is correct
37 Correct 533 ms 24736 KB Output is correct
38 Correct 611 ms 23068 KB Output is correct
39 Correct 624 ms 22532 KB Output is correct
40 Correct 692 ms 18936 KB Output is correct
41 Correct 815 ms 17120 KB Output is correct
42 Correct 847 ms 17472 KB Output is correct
43 Correct 333 ms 24780 KB Output is correct
44 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '1050'
45 Halted 0 ms 0 KB -