#include<bits/stdc++.h>
#define MAXN 1000007
using namespace std;
int n,s,p[MAXN],fr,last,mins,maxs,pref[MAXN],curr,A,B;
bool li[MAXN];
long long ans;
int k,cycle[MAXN];
pair<int,int> where[MAXN];
void dfs(int x){
li[x]=true;
cycle[x]=k;
ans+=abs(x-p[x]);
mins=min(mins,x);
maxs=max(maxs,x);
if(!li[p[x]])dfs(p[x]);
}
int dp[1007][1007];
bool vis[1007][1007];
int ff(int l,int r){
if(l<=A and r>=B)return 0;
if(vis[l][r])return dp[l][r];
vis[l][r]=true;
dp[l][r]=2*n;
if(l>A){
int from=l-1,to=r,ll=l-1,rr=r+1;
while(ll>=from or rr<=to){
while(ll>=from){
from=min(from,where[cycle[ll]].first);
to=max(to,where[cycle[ll]].second);
ll--;
}
while(rr<=to){
from=min(from,where[cycle[rr]].first);
to=max(to,where[cycle[rr]].second);
rr++;
}
}
dp[l][r]=min(dp[l][r],ff(from,to)+2);
}
if(r<B){
int from=l,to=r+1,ll=l-1,rr=r+1;
while(ll>=from or rr<=to){
while(ll>=from){
from=min(from,where[cycle[ll]].first);
to=max(to,where[cycle[ll]].second);
ll--;
}
while(rr<=to){
from=min(from,where[cycle[rr]].first);
to=max(to,where[cycle[rr]].second);
rr++;
}
}
dp[l][r]=min(dp[l][r],ff(from,to)+2);
}
return dp[l][r];
}
long long minimum_walk(vector<int> P, int S){
n=int(P.size()); s=S+1;
for(int i=1;i<=n;i++){
p[i]=P[i-1]+1;
li[i]=false; pref[i]=0;
}
last=B=0; fr=A=n+1; k=0; ans=0;
for(int i=1;i<=n;i++){
if(p[i]!=i)B=i;
if(!li[i]){
if(p[i]!=i)last=i;
dfs(i);
}
}
for(int i=1;i<=n;i++)li[i]=false;
for(int i=n;i>=1;i--){
if(p[i]!=i)A=i;
if(!li[i]){
k++;
if(p[i]!=i)fr=i;
mins=n; maxs=1;
dfs(i);
where[k]={mins,maxs};
pref[mins]++; pref[maxs]--;
}
}
ans/=2;
/// more steps inside initial cycle
if(s!=1){
int from=s,to=s,ll=s,rr=s;
while(ll>=from or rr<=to){
while(ll>=from){
from=min(from,where[cycle[ll]].first);
to=max(to,where[cycle[ll]].second);
ll--;
}
while(rr<=to){
from=min(from,where[cycle[rr]].first);
to=max(to,where[cycle[rr]].second);
rr++;
}
}
ans+=ff(from,to);
}else{
for(int i=1;i<=n-1;i++){
curr+=pref[i];
if(i<s)continue;
if(curr==0 and last>i)ans+=2;
}
curr=0;
for(int i=n;i>=2;i--){
curr+=pref[i];
if(i>s)continue;
if(curr==0 and fr<i)ans+=2;
}
}
return ans;
}
/*int main(){
vector<int> perm;
for(int i=0;i<10;i++){
perm.push_back(i);
}
while(true){
random_shuffle(perm.begin(),perm.end());
for(int i=0;i<10;i++){
cout<<perm[i]<<" ";
}
cout<<"\n";
int z=rand()%10;
cout<<z<<"\n";
minimum_walk(perm,z);
}
return 0;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Correct |
2 ms |
8528 KB |
Output is correct |
4 |
Correct |
1 ms |
8528 KB |
Output is correct |
5 |
Correct |
1 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
1 ms |
8556 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
1 ms |
8528 KB |
Output is correct |
12 |
Correct |
2 ms |
8528 KB |
Output is correct |
13 |
Correct |
2 ms |
8528 KB |
Output is correct |
14 |
Correct |
2 ms |
8528 KB |
Output is correct |
15 |
Correct |
1 ms |
8528 KB |
Output is correct |
16 |
Correct |
2 ms |
8640 KB |
Output is correct |
17 |
Correct |
2 ms |
8528 KB |
Output is correct |
18 |
Correct |
2 ms |
8528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Correct |
2 ms |
8528 KB |
Output is correct |
4 |
Correct |
1 ms |
8528 KB |
Output is correct |
5 |
Correct |
1 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
1 ms |
8556 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
1 ms |
8528 KB |
Output is correct |
12 |
Correct |
2 ms |
8528 KB |
Output is correct |
13 |
Correct |
2 ms |
8528 KB |
Output is correct |
14 |
Correct |
2 ms |
8528 KB |
Output is correct |
15 |
Correct |
1 ms |
8528 KB |
Output is correct |
16 |
Correct |
2 ms |
8640 KB |
Output is correct |
17 |
Correct |
2 ms |
8528 KB |
Output is correct |
18 |
Correct |
2 ms |
8528 KB |
Output is correct |
19 |
Correct |
2 ms |
8528 KB |
Output is correct |
20 |
Correct |
2 ms |
8528 KB |
Output is correct |
21 |
Correct |
2 ms |
8528 KB |
Output is correct |
22 |
Correct |
2 ms |
8528 KB |
Output is correct |
23 |
Correct |
2 ms |
8528 KB |
Output is correct |
24 |
Correct |
2 ms |
8528 KB |
Output is correct |
25 |
Correct |
2 ms |
8692 KB |
Output is correct |
26 |
Correct |
2 ms |
8528 KB |
Output is correct |
27 |
Correct |
2 ms |
8528 KB |
Output is correct |
28 |
Correct |
2 ms |
8696 KB |
Output is correct |
29 |
Correct |
2 ms |
8528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Correct |
2 ms |
8528 KB |
Output is correct |
4 |
Correct |
1 ms |
8528 KB |
Output is correct |
5 |
Correct |
1 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
1 ms |
8556 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
1 ms |
8528 KB |
Output is correct |
12 |
Correct |
2 ms |
8528 KB |
Output is correct |
13 |
Correct |
2 ms |
8528 KB |
Output is correct |
14 |
Correct |
2 ms |
8528 KB |
Output is correct |
15 |
Correct |
1 ms |
8528 KB |
Output is correct |
16 |
Correct |
2 ms |
8640 KB |
Output is correct |
17 |
Correct |
2 ms |
8528 KB |
Output is correct |
18 |
Correct |
2 ms |
8528 KB |
Output is correct |
19 |
Correct |
2 ms |
8528 KB |
Output is correct |
20 |
Correct |
2 ms |
8528 KB |
Output is correct |
21 |
Correct |
2 ms |
8528 KB |
Output is correct |
22 |
Correct |
2 ms |
8528 KB |
Output is correct |
23 |
Correct |
2 ms |
8528 KB |
Output is correct |
24 |
Correct |
2 ms |
8528 KB |
Output is correct |
25 |
Correct |
2 ms |
8692 KB |
Output is correct |
26 |
Correct |
2 ms |
8528 KB |
Output is correct |
27 |
Correct |
2 ms |
8528 KB |
Output is correct |
28 |
Correct |
2 ms |
8696 KB |
Output is correct |
29 |
Correct |
2 ms |
8528 KB |
Output is correct |
30 |
Correct |
121 ms |
23292 KB |
Output is correct |
31 |
Correct |
118 ms |
23292 KB |
Output is correct |
32 |
Correct |
99 ms |
29436 KB |
Output is correct |
33 |
Correct |
104 ms |
27208 KB |
Output is correct |
34 |
Correct |
137 ms |
27396 KB |
Output is correct |
35 |
Correct |
110 ms |
27392 KB |
Output is correct |
36 |
Correct |
92 ms |
25156 KB |
Output is correct |
37 |
Correct |
84 ms |
23112 KB |
Output is correct |
38 |
Correct |
77 ms |
23292 KB |
Output is correct |
39 |
Correct |
79 ms |
23112 KB |
Output is correct |
40 |
Correct |
82 ms |
23292 KB |
Output is correct |
41 |
Correct |
113 ms |
23288 KB |
Output is correct |
42 |
Correct |
105 ms |
23112 KB |
Output is correct |
43 |
Correct |
121 ms |
29444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12112 KB |
Output is correct |
2 |
Correct |
2 ms |
10576 KB |
Output is correct |
3 |
Correct |
3 ms |
11600 KB |
Output is correct |
4 |
Correct |
4 ms |
12112 KB |
Output is correct |
5 |
Correct |
4 ms |
12112 KB |
Output is correct |
6 |
Correct |
2 ms |
10576 KB |
Output is correct |
7 |
Correct |
2 ms |
10576 KB |
Output is correct |
8 |
Correct |
2 ms |
10576 KB |
Output is correct |
9 |
Correct |
2 ms |
10576 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
3 ms |
12368 KB |
Output is correct |
12 |
Correct |
3 ms |
12368 KB |
Output is correct |
13 |
Correct |
2 ms |
11344 KB |
Output is correct |
14 |
Correct |
2 ms |
11088 KB |
Output is correct |
15 |
Correct |
2 ms |
10832 KB |
Output is correct |
16 |
Correct |
2 ms |
10628 KB |
Output is correct |
17 |
Correct |
3 ms |
12368 KB |
Output is correct |
18 |
Correct |
4 ms |
12112 KB |
Output is correct |
19 |
Correct |
2 ms |
11600 KB |
Output is correct |
20 |
Correct |
2 ms |
10576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Correct |
2 ms |
8528 KB |
Output is correct |
4 |
Correct |
1 ms |
8528 KB |
Output is correct |
5 |
Correct |
1 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
1 ms |
8556 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
1 ms |
8528 KB |
Output is correct |
12 |
Correct |
2 ms |
8528 KB |
Output is correct |
13 |
Correct |
2 ms |
8528 KB |
Output is correct |
14 |
Correct |
2 ms |
8528 KB |
Output is correct |
15 |
Correct |
1 ms |
8528 KB |
Output is correct |
16 |
Correct |
2 ms |
8640 KB |
Output is correct |
17 |
Correct |
2 ms |
8528 KB |
Output is correct |
18 |
Correct |
2 ms |
8528 KB |
Output is correct |
19 |
Correct |
2 ms |
8528 KB |
Output is correct |
20 |
Correct |
2 ms |
8528 KB |
Output is correct |
21 |
Correct |
2 ms |
8528 KB |
Output is correct |
22 |
Correct |
2 ms |
8528 KB |
Output is correct |
23 |
Correct |
2 ms |
8528 KB |
Output is correct |
24 |
Correct |
2 ms |
8528 KB |
Output is correct |
25 |
Correct |
2 ms |
8692 KB |
Output is correct |
26 |
Correct |
2 ms |
8528 KB |
Output is correct |
27 |
Correct |
2 ms |
8528 KB |
Output is correct |
28 |
Correct |
2 ms |
8696 KB |
Output is correct |
29 |
Correct |
2 ms |
8528 KB |
Output is correct |
30 |
Correct |
121 ms |
23292 KB |
Output is correct |
31 |
Correct |
118 ms |
23292 KB |
Output is correct |
32 |
Correct |
99 ms |
29436 KB |
Output is correct |
33 |
Correct |
104 ms |
27208 KB |
Output is correct |
34 |
Correct |
137 ms |
27396 KB |
Output is correct |
35 |
Correct |
110 ms |
27392 KB |
Output is correct |
36 |
Correct |
92 ms |
25156 KB |
Output is correct |
37 |
Correct |
84 ms |
23112 KB |
Output is correct |
38 |
Correct |
77 ms |
23292 KB |
Output is correct |
39 |
Correct |
79 ms |
23112 KB |
Output is correct |
40 |
Correct |
82 ms |
23292 KB |
Output is correct |
41 |
Correct |
113 ms |
23288 KB |
Output is correct |
42 |
Correct |
105 ms |
23112 KB |
Output is correct |
43 |
Correct |
121 ms |
29444 KB |
Output is correct |
44 |
Correct |
4 ms |
12112 KB |
Output is correct |
45 |
Correct |
2 ms |
10576 KB |
Output is correct |
46 |
Correct |
3 ms |
11600 KB |
Output is correct |
47 |
Correct |
4 ms |
12112 KB |
Output is correct |
48 |
Correct |
4 ms |
12112 KB |
Output is correct |
49 |
Correct |
2 ms |
10576 KB |
Output is correct |
50 |
Correct |
2 ms |
10576 KB |
Output is correct |
51 |
Correct |
2 ms |
10576 KB |
Output is correct |
52 |
Correct |
2 ms |
10576 KB |
Output is correct |
53 |
Correct |
2 ms |
8528 KB |
Output is correct |
54 |
Correct |
3 ms |
12368 KB |
Output is correct |
55 |
Correct |
3 ms |
12368 KB |
Output is correct |
56 |
Correct |
2 ms |
11344 KB |
Output is correct |
57 |
Correct |
2 ms |
11088 KB |
Output is correct |
58 |
Correct |
2 ms |
10832 KB |
Output is correct |
59 |
Correct |
2 ms |
10628 KB |
Output is correct |
60 |
Correct |
3 ms |
12368 KB |
Output is correct |
61 |
Correct |
4 ms |
12112 KB |
Output is correct |
62 |
Correct |
2 ms |
11600 KB |
Output is correct |
63 |
Correct |
2 ms |
10576 KB |
Output is correct |
64 |
Runtime error |
129 ms |
66160 KB |
Execution killed with signal 11 |
65 |
Halted |
0 ms |
0 KB |
- |