#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]=cr[cnt]=i;
while(true){
c[x]=cnt;
cl[cnt]=min(cl[cnt],x);
cr[cnt]=max(cr[cnt],x);
ans+=abs(x-p[x]);
x=p[x];
if(x==i)break;
}
if(x!=p[x]){
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 |
0 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 |
1 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 |
0 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 |
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 |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 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 |
0 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 |
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 |
0 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 |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 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 |
0 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 |
1 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 |
0 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 |
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 |
0 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 |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
123 ms |
12048 KB |
Output is correct |
31 |
Correct |
152 ms |
12028 KB |
Output is correct |
32 |
Correct |
129 ms |
19836 KB |
Output is correct |
33 |
Correct |
100 ms |
17400 KB |
Output is correct |
34 |
Correct |
106 ms |
17400 KB |
Output is correct |
35 |
Correct |
112 ms |
16244 KB |
Output is correct |
36 |
Correct |
98 ms |
13960 KB |
Output is correct |
37 |
Correct |
94 ms |
12272 KB |
Output is correct |
38 |
Correct |
104 ms |
12020 KB |
Output is correct |
39 |
Correct |
100 ms |
11980 KB |
Output is correct |
40 |
Correct |
100 ms |
12016 KB |
Output is correct |
41 |
Correct |
109 ms |
12028 KB |
Output is correct |
42 |
Correct |
113 ms |
12084 KB |
Output is correct |
43 |
Correct |
102 ms |
17852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
Execution timed out |
2073 ms |
212 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 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 |
0 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 |
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 |
0 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 |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
123 ms |
12048 KB |
Output is correct |
31 |
Correct |
152 ms |
12028 KB |
Output is correct |
32 |
Correct |
129 ms |
19836 KB |
Output is correct |
33 |
Correct |
100 ms |
17400 KB |
Output is correct |
34 |
Correct |
106 ms |
17400 KB |
Output is correct |
35 |
Correct |
112 ms |
16244 KB |
Output is correct |
36 |
Correct |
98 ms |
13960 KB |
Output is correct |
37 |
Correct |
94 ms |
12272 KB |
Output is correct |
38 |
Correct |
104 ms |
12020 KB |
Output is correct |
39 |
Correct |
100 ms |
11980 KB |
Output is correct |
40 |
Correct |
100 ms |
12016 KB |
Output is correct |
41 |
Correct |
109 ms |
12028 KB |
Output is correct |
42 |
Correct |
113 ms |
12084 KB |
Output is correct |
43 |
Correct |
102 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 |
0 ms |
340 KB |
Output is correct |
47 |
Execution timed out |
2073 ms |
212 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |