Submission #832496

# Submission time Handle Problem Language Result Execution time Memory
832496 2023-08-21T10:54:32 Z Rafi22 Ancient Books (IOI17_books) C++14
100 / 100
132 ms 27988 KB
#include <bits/stdc++.h>
     
using namespace std;
     
#define ll long long 
#define ld long double
#define pb push_back
#define st first
#define nd second
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
int inf=1000000007;
ll infl=1000000000000000007;
     
const int N=1000007;
     
bool odw[N];
int id[N];
vector<pair<int,int>>V;
    
    
ll minimum_walk(vector<int>p,int s)
{
	int n=sz(p);
	ll ans=0;
	for(int i=0;i<n;i++) ans+=abs(i-p[i]);
	int c=0;
	int A=0,B=n-1;
	while(A<s&&p[A]==A) A++;
	while(s<B&&p[B]==B) B--;
	for(int i=0;i<n;i++)
	{
		if(odw[i]) continue;
		int l=inf,r=-inf,x=i;
		vector<int>X;
		while(!odw[x])
		{
			odw[x]=1;
			X.pb(x);
			l=min(l,x);
			r=max(r,x);
			x=p[x];
		}
		if(l==r&&(l<A||l>B)) continue;   		
		V.pb({l,r});
		for(auto x:X) id[x]=c;
		c++;
	}
	int i=s,j=s;
	int l=V[id[s]].st,r=V[id[s]].nd;
	while(l>A||r<B)
	{
		while(true)
		{
			if(i>l) 
			{
				i--;
				l=min(l,V[id[i]].st);
				r=max(r,V[id[i]].nd);
				
			}
			else if(j<r)
			{
				j++;
				l=min(l,V[id[j]].st);
				r=max(r,V[id[j]].nd);
			}
			else break;
		}
		//cout<<l<<" "<<r<<endl;
		if(l==A&&r==B) break;
		int cL=0,L=-1,p=inf;
		for(int k=l;k>=A;k--)
		{
			p=min(p,V[id[k]].st);
			if(V[id[k]].nd>r)
			{
				 L=k;
				 break;
			}
			if(k!=A&&p==k) cL++;
		}
		p=-inf;
		int cR=0,R=-1;
		for(int k=r;k<=B;k++)
		{
			p=max(p,V[id[k]].nd);
			if(V[id[k]].st<l)
			{
				 R=k;
				 break;
			}
			if(k!=B&&p==k) cR++;
		}
		if(L==-1)
		{
			ans+=2*(cL+cR);
			break;
		}
		l=L;
		r=R;
		ans+=2*min(cL,cR);
	}
    	
	return ans;
}
/*
int main()
{
	int nn,s;
	cin>>nn>>s;
	vector<int>V(nn);
	for(int i=0;i<nn;i++) cin>>V[i];
	//random_shuffle(all(V));
	//for(int i=0;i<nn;i++) cout<<V[i]<<" ";
	//cout<<endl;
	cout<<minimum_walk(V,s);
	return 0;
}*/
    
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 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 1 ms 308 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 304 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 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 1 ms 308 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 304 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 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 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 308 KB Output is correct
23 Correct 0 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 308 KB Output is correct
27 Correct 0 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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 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 1 ms 308 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 304 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 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 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 308 KB Output is correct
23 Correct 0 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 308 KB Output is correct
27 Correct 0 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 110 ms 22940 KB Output is correct
31 Correct 103 ms 21744 KB Output is correct
32 Correct 96 ms 15816 KB Output is correct
33 Correct 122 ms 26916 KB Output is correct
34 Correct 117 ms 26840 KB Output is correct
35 Correct 114 ms 27856 KB Output is correct
36 Correct 120 ms 21840 KB Output is correct
37 Correct 88 ms 20272 KB Output is correct
38 Correct 85 ms 19808 KB Output is correct
39 Correct 87 ms 19808 KB Output is correct
40 Correct 86 ms 19956 KB Output is correct
41 Correct 99 ms 20584 KB Output is correct
42 Correct 103 ms 20644 KB Output is correct
43 Correct 112 ms 27704 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 340 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 0 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 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 304 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 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 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 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 1 ms 308 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 304 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 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 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 308 KB Output is correct
23 Correct 0 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 308 KB Output is correct
27 Correct 0 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 110 ms 22940 KB Output is correct
31 Correct 103 ms 21744 KB Output is correct
32 Correct 96 ms 15816 KB Output is correct
33 Correct 122 ms 26916 KB Output is correct
34 Correct 117 ms 26840 KB Output is correct
35 Correct 114 ms 27856 KB Output is correct
36 Correct 120 ms 21840 KB Output is correct
37 Correct 88 ms 20272 KB Output is correct
38 Correct 85 ms 19808 KB Output is correct
39 Correct 87 ms 19808 KB Output is correct
40 Correct 86 ms 19956 KB Output is correct
41 Correct 99 ms 20584 KB Output is correct
42 Correct 103 ms 20644 KB Output is correct
43 Correct 112 ms 27704 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 340 KB Output is correct
47 Correct 1 ms 308 KB Output is correct
48 Correct 0 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 340 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 312 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 304 KB Output is correct
57 Correct 1 ms 308 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 0 ms 340 KB Output is correct
61 Correct 1 ms 340 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 114 ms 26712 KB Output is correct
65 Correct 108 ms 26424 KB Output is correct
66 Correct 87 ms 20192 KB Output is correct
67 Correct 87 ms 19948 KB Output is correct
68 Correct 10 ms 2504 KB Output is correct
69 Correct 10 ms 2380 KB Output is correct
70 Correct 10 ms 2768 KB Output is correct
71 Correct 12 ms 3244 KB Output is correct
72 Correct 11 ms 3148 KB Output is correct
73 Correct 9 ms 2236 KB Output is correct
74 Correct 112 ms 26828 KB Output is correct
75 Correct 132 ms 26848 KB Output is correct
76 Correct 114 ms 27980 KB Output is correct
77 Correct 110 ms 27856 KB Output is correct
78 Correct 125 ms 27216 KB Output is correct
79 Correct 120 ms 27224 KB Output is correct
80 Correct 113 ms 15712 KB Output is correct
81 Correct 103 ms 23736 KB Output is correct
82 Correct 104 ms 23764 KB Output is correct
83 Correct 107 ms 23884 KB Output is correct
84 Correct 112 ms 23900 KB Output is correct
85 Correct 97 ms 21848 KB Output is correct
86 Correct 108 ms 20316 KB Output is correct
87 Correct 113 ms 27980 KB Output is correct
88 Correct 114 ms 27988 KB Output is correct
89 Correct 118 ms 23968 KB Output is correct
90 Correct 94 ms 19936 KB Output is correct
91 Correct 89 ms 19808 KB Output is correct
92 Correct 88 ms 19824 KB Output is correct