#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define sz(v) ((int)(v).size())
typedef long long lint;
int b,n,sz,cnt;
int v[1000002],rev[1000002];
vector<int>go;
lint ans=0;
void dfs(int p,int q)
{
if(b==-1)
{
ans+=p;
return ;
}
int r=p;
if(q!=b)
{
r=rev[b];
ans+=abs(r-p);
if(q!=-1)rev[q]=r;
}
ans+=abs(b-r);
int tmp=b;
q=go[b];
go[b]=b;rev[b]=b;
while(b-1>=0 && go[b-1]==b-1)b--;
b--;
dfs(tmp,q);
}
lint minimum_walk(vector<int> p, int s) {
n=sz(p);
go=p;
for(int i=0;i<n;i++)rev[go[i]]=i;
b=n;
while(b-1>=0 && go[b-1]==b-1)b--;
b--;
if(b==-1)return 0;
int t=-1,maxi=-1;
for(int i=rev[b]-1;i>=0;i--)
{
if(go[i]!=i)
{
if(maxi<go[i])
{
maxi=go[i];
t=i;
}
}
}
if(t==-1)t=b;
ans+=t;
maxi=go[t];
go[t]=-1;
dfs(t,maxi);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '10' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '10' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '10' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4780' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '10' |
3 |
Halted |
0 ms |
0 KB |
- |