Submission #560328

# Submission time Handle Problem Language Result Execution time Memory
560328 2022-05-11T09:54:03 Z jamezzz Ancient Books (IOI17_books) C++17
100 / 100
135 ms 26728 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;

#define maxn 1000005

int n,cl[maxn],cr[maxn],c[maxn];

void extend(int &l,int &r){
	//printf("extend: %d %d\n",l,r);
	int farl=min(cl[c[l]],cl[c[r]]);
	int farr=max(cr[c[l]],cr[c[r]]);
	while(true){
		if(farl<l){
			--l;
			farl=min(farl,cl[c[l]]);
			farr=max(farr,cr[c[l]]);
		}
		else if(r<farr){
			++r;
			farl=min(farl,cl[c[r]]);
			farr=max(farr,cr[c[r]]);
		}
		else break;
	}
	//printf("res: %d %d\n",l,r);
}

ll minimum_walk(vector<int> p,int s){
	n=p.size();
	int lbound=s,rbound=s;
	ll ans=0;int cnt=1;
	for(int i=0;i<n;++i){
		if(c[i]!=0)continue;
		int x=i;
		cl[cnt]=i;
		while(true){
			c[x]=cnt;
			cr[cnt]=max(cr[cnt],x);
			ans+=abs(x-p[x]);
			x=p[x];
			if(x==i)break;
		}
		if(i!=p[i]){
			lbound=min(lbound,cl[cnt]);
			rbound=max(rbound,cr[cnt]);
		}
		++cnt;
	}
	
	int l=s,r=s;
	while(l!=lbound||r!=rbound){
		//printf("%d %d\n",l,r);
		extend(l,r);
		bool can=false;
		int newl=n,newr=0;
		
		int tmpl=l,tmpr=r;
		int lcost=0;
		while(true){
			if(tmpl<=lbound)break;//done
			--tmpl;lcost+=2;
			extend(tmpl,tmpr);
			if(tmpr>r){
				can=true;
				break;
			}
		}
		newl=min(newl,tmpl);
		newr=max(newr,tmpr);
		
		tmpl=l,tmpr=r;
		int rcost=0;
		while(true){
			if(tmpr>=rbound)break;//done
			++tmpr;rcost+=2;
			extend(tmpl,tmpr);
			if(tmpl<l){
				can=true;
				break;
			}
		}
		newl=min(newl,tmpl);
		newr=max(newr,tmpr);
		
		if(can)ans+=min(lcost,rcost);
		else ans+=lcost+rcost;
		
		l=newl,r=newr;
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 131 ms 12044 KB Output is correct
31 Correct 135 ms 12012 KB Output is correct
32 Correct 92 ms 19836 KB Output is correct
33 Correct 114 ms 17392 KB Output is correct
34 Correct 131 ms 17396 KB Output is correct
35 Correct 106 ms 16372 KB Output is correct
36 Correct 103 ms 13936 KB Output is correct
37 Correct 100 ms 12280 KB Output is correct
38 Correct 96 ms 12016 KB Output is correct
39 Correct 95 ms 12032 KB Output is correct
40 Correct 115 ms 12020 KB Output is correct
41 Correct 118 ms 12016 KB Output is correct
42 Correct 109 ms 12020 KB Output is correct
43 Correct 96 ms 17852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 308 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 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 308 KB Output is correct
20 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 131 ms 12044 KB Output is correct
31 Correct 135 ms 12012 KB Output is correct
32 Correct 92 ms 19836 KB Output is correct
33 Correct 114 ms 17392 KB Output is correct
34 Correct 131 ms 17396 KB Output is correct
35 Correct 106 ms 16372 KB Output is correct
36 Correct 103 ms 13936 KB Output is correct
37 Correct 100 ms 12280 KB Output is correct
38 Correct 96 ms 12016 KB Output is correct
39 Correct 95 ms 12032 KB Output is correct
40 Correct 115 ms 12020 KB Output is correct
41 Correct 118 ms 12016 KB Output is correct
42 Correct 109 ms 12020 KB Output is correct
43 Correct 96 ms 17852 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 308 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 212 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 1 ms 340 KB Output is correct
62 Correct 1 ms 308 KB Output is correct
63 Correct 1 ms 308 KB Output is correct
64 Correct 109 ms 24388 KB Output is correct
65 Correct 116 ms 25068 KB Output is correct
66 Correct 99 ms 18968 KB Output is correct
67 Correct 105 ms 18944 KB Output is correct
68 Correct 12 ms 2216 KB Output is correct
69 Correct 15 ms 2116 KB Output is correct
70 Correct 14 ms 2352 KB Output is correct
71 Correct 12 ms 2492 KB Output is correct
72 Correct 15 ms 2744 KB Output is correct
73 Correct 10 ms 1984 KB Output is correct
74 Correct 107 ms 24140 KB Output is correct
75 Correct 117 ms 24180 KB Output is correct
76 Correct 111 ms 24428 KB Output is correct
77 Correct 113 ms 24556 KB Output is correct
78 Correct 109 ms 23668 KB Output is correct
79 Correct 106 ms 23784 KB Output is correct
80 Correct 116 ms 26728 KB Output is correct
81 Correct 118 ms 22660 KB Output is correct
82 Correct 104 ms 22868 KB Output is correct
83 Correct 112 ms 21748 KB Output is correct
84 Correct 111 ms 21100 KB Output is correct
85 Correct 101 ms 19876 KB Output is correct
86 Correct 119 ms 19300 KB Output is correct
87 Correct 117 ms 25296 KB Output is correct
88 Correct 110 ms 24312 KB Output is correct
89 Correct 104 ms 22388 KB Output is correct
90 Correct 106 ms 18936 KB Output is correct
91 Correct 113 ms 18792 KB Output is correct
92 Correct 111 ms 18672 KB Output is correct