#include "books.h"
#include <bits/stdc++.h>
using namespace std;
int vis[1000001],r[1000001],g[1000001],nxt[1000001];
vector <pair <int, pair <int, int>>> e;
long long minimum_walk(vector <int> p, int s){
int n=p.size(),lo=n,hi=0;
iota(nxt,nxt+n,0);
vector <pair <int, int>> tmp;
long long d=0;
for (int i=0;i<n;i++)
if (!vis[i]){
int x=i;
while (!vis[x]){
vis[x]=1;
g[x]=i;
r[i]=max(r[i],x);
d+=abs(x-p[x]);
x=p[x];
}
if (r[i]!=i||i==s){
lo=min(lo,i);
hi=max(hi,r[i]);
}
}
int L=g[s],R=r[g[s]];
while (L>lo||R<hi){
if (nxt[L]!=R){
int j=nxt[L]+1;
nxt[L]=nxt[j];
L=min(L,g[j]);
R=max(R,r[g[j]]);
continue;
}
int tmpl=L,tmpr=R;
while (L>=lo||R<=hi){
d+=2;
L=max(L,lo)-1;
R=min(R,hi)+1;
int newL=(L<lo?lo:g[L]),newR=(R>hi?hi:r[g[R]]),ch=0;
while (L>newL){
if (L>=lo&&r[g[L]]>tmpr){
L=g[L];
R=r[g[L]];
ch=1;
break;
}
L--;
}
if (ch)
break;
ch=0;
while (R<newR){
if (R<=hi&&g[R]<tmpl){
L=g[R];
R=r[g[R]];
ch=1;
break;
}
R++;
}
if (ch)
break;
}
}
return d;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3310' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |