#include<bits/stdc++.h>
#include "books.h"
using namespace std;
#define vi vector<int>
#define REP(i,n) for(int i = 0; i < n; i ++)
#define FOR(i,a,b) for(int i = a; i < b; i ++)
const int MX = 1000005;
int n,S;
int lft,rgt,cur = 0;
int C[MX],L[MX],R[MX];
int solve(int l,int r){
int ll = L[C[l]];
int rr = R[C[r]];
while(ll < l){
l--;
ll = min(ll,L[C[l]]);
}
while(rr > r){
r++;
rr = max(rr,R[C[r]]);
}
int costleft = 0,costright = 0;
int indleft = -1,indright = -1;
int mn = l,mx = r;
for(int i = l-1; i >= lft; i --){
if(R[C[i]] > S){
indleft = i;
break;
}
}
for(int i = r+1; i <= rgt; i ++){
if(L[C[i]] < S){
indright = i;
break;
}
}
if(indleft == -1){
for(int i = l-1; i >= lft; i --){
mn = min(mn,L[C[i]]);
if(mn == i) costleft += 2;
}
for(int i = r+1; i <= rgt; i ++){
mx = max(mx,R[C[i]]);
if(mx == i) costright += 2;
}
return costleft+costright;
}
while(ll > indleft){
costleft += 2;
ll--;
while(ll < l){
l--;
ll = min(ll,L[C[l]]);
}
}
while(rr < indright){
costright += 2;
rr++;
while(rr > r){
r++;
rr = max(rr,R[C[r]]);
}
}
return min(costleft,costright)+solve(ll,rr);
}
long long minimum_walk(vi p, int s) {
S = s;
long long ans = 0;
n = p.size();
lft = s;
rgt = s;
REP(i,n) C[i] = -1;
REP(i,n){
ans += abs(p[i]-i);
if(C[i] == -1){
C[i] = cur;
L[cur] = R[cur] = i;
int j = i;
while(true){
j = p[j];
C[j] = cur;
R[cur] = max(R[cur],j);
if(i == j) break;
}
cur++;
}
if(i != p[i]){
lft = min(lft,i);
rgt = max(rgt,i);
}
}
return ans+solve(s,s);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
296 KB |
Output is correct |
29 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
296 KB |
Output is correct |
29 |
Correct |
1 ms |
304 KB |
Output is correct |
30 |
Correct |
170 ms |
15912 KB |
Output is correct |
31 |
Correct |
170 ms |
15948 KB |
Output is correct |
32 |
Correct |
128 ms |
23796 KB |
Output is correct |
33 |
Correct |
161 ms |
21344 KB |
Output is correct |
34 |
Correct |
132 ms |
21380 KB |
Output is correct |
35 |
Correct |
132 ms |
20208 KB |
Output is correct |
36 |
Correct |
130 ms |
18076 KB |
Output is correct |
37 |
Correct |
155 ms |
16208 KB |
Output is correct |
38 |
Correct |
128 ms |
16000 KB |
Output is correct |
39 |
Correct |
131 ms |
15940 KB |
Output is correct |
40 |
Correct |
133 ms |
15908 KB |
Output is correct |
41 |
Correct |
145 ms |
16056 KB |
Output is correct |
42 |
Correct |
145 ms |
15984 KB |
Output is correct |
43 |
Correct |
133 ms |
21760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2748' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
296 KB |
Output is correct |
29 |
Correct |
1 ms |
304 KB |
Output is correct |
30 |
Correct |
170 ms |
15912 KB |
Output is correct |
31 |
Correct |
170 ms |
15948 KB |
Output is correct |
32 |
Correct |
128 ms |
23796 KB |
Output is correct |
33 |
Correct |
161 ms |
21344 KB |
Output is correct |
34 |
Correct |
132 ms |
21380 KB |
Output is correct |
35 |
Correct |
132 ms |
20208 KB |
Output is correct |
36 |
Correct |
130 ms |
18076 KB |
Output is correct |
37 |
Correct |
155 ms |
16208 KB |
Output is correct |
38 |
Correct |
128 ms |
16000 KB |
Output is correct |
39 |
Correct |
131 ms |
15940 KB |
Output is correct |
40 |
Correct |
133 ms |
15908 KB |
Output is correct |
41 |
Correct |
145 ms |
16056 KB |
Output is correct |
42 |
Correct |
145 ms |
15984 KB |
Output is correct |
43 |
Correct |
133 ms |
21760 KB |
Output is correct |
44 |
Incorrect |
1 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2748' |
45 |
Halted |
0 ms |
0 KB |
- |