#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[1001000], n;
bool visited[1001000];
ll myabs(ll x){
if (x<0) return -x;
return x;
}
ll solve(int l, int r, int s){
if (l>r) return 0;
ll ret = 0;
int mn = l;
int mx = l;
int ptl = l, ptr = l;
while(mn <= ptl && ptr <= mx){
int j = -1;
if (mn <= ptl){
if (visited[ptl]) {--ptl; continue;}
j = ptl--;
}
else{
if (visited[ptr]) {++ptr; continue;}
j = ptr++;
}
for (;!visited[j];j=a[j]){
visited[j] = 1;
ret += myabs(j-a[j]);
mn = min(mn, j);
mx = max(mx, j);
}
}
ll ret2 = solve(mx+1, r, mx+1);
ll ret3 = solve(l, mn-1, mn-1);
return ret + (ret2 ? ret2+2 : 0) + (ret3 ? ret3+2 : 0);
}
long long minimum_walk(std::vector<int> P, int S) {
n = P.size();
for (int i=1;i<=n;i++) a[i] = P[i-1] + 1;
return solve(1, n, S+1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '6' |
7 |
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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '6' |
7 |
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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '6' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2000' |
2 |
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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '6' |
7 |
Halted |
0 ms |
0 KB |
- |