#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=tmpl,r=tmpr;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
308 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 |
304 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
304 KB |
Output is correct |
25 |
Correct |
1 ms |
308 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
308 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 |
304 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
304 KB |
Output is correct |
25 |
Correct |
1 ms |
308 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
166 ms |
12236 KB |
Output is correct |
31 |
Correct |
128 ms |
12212 KB |
Output is correct |
32 |
Correct |
106 ms |
20112 KB |
Output is correct |
33 |
Correct |
109 ms |
17668 KB |
Output is correct |
34 |
Correct |
109 ms |
17632 KB |
Output is correct |
35 |
Correct |
119 ms |
16532 KB |
Output is correct |
36 |
Correct |
108 ms |
14248 KB |
Output is correct |
37 |
Correct |
103 ms |
12560 KB |
Output is correct |
38 |
Correct |
101 ms |
12236 KB |
Output is correct |
39 |
Correct |
104 ms |
12240 KB |
Output is correct |
40 |
Correct |
99 ms |
12184 KB |
Output is correct |
41 |
Correct |
114 ms |
12296 KB |
Output is correct |
42 |
Correct |
117 ms |
12184 KB |
Output is correct |
43 |
Correct |
107 ms |
17996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Execution timed out |
2078 ms |
308 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
308 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 |
304 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
304 KB |
Output is correct |
25 |
Correct |
1 ms |
308 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
166 ms |
12236 KB |
Output is correct |
31 |
Correct |
128 ms |
12212 KB |
Output is correct |
32 |
Correct |
106 ms |
20112 KB |
Output is correct |
33 |
Correct |
109 ms |
17668 KB |
Output is correct |
34 |
Correct |
109 ms |
17632 KB |
Output is correct |
35 |
Correct |
119 ms |
16532 KB |
Output is correct |
36 |
Correct |
108 ms |
14248 KB |
Output is correct |
37 |
Correct |
103 ms |
12560 KB |
Output is correct |
38 |
Correct |
101 ms |
12236 KB |
Output is correct |
39 |
Correct |
104 ms |
12240 KB |
Output is correct |
40 |
Correct |
99 ms |
12184 KB |
Output is correct |
41 |
Correct |
114 ms |
12296 KB |
Output is correct |
42 |
Correct |
117 ms |
12184 KB |
Output is correct |
43 |
Correct |
107 ms |
17996 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
304 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Execution timed out |
2078 ms |
308 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |