#include<bits/stdc++.h>
using namespace std;
#define for2(a,b,c) for(int a = b; a < c; a++)
#include "books.h"
#define ll long long
const int maxn = 1000100;
int par[maxn];
bool seen[maxn];
int getpar(int x){
if(par[x] < 0) return x;
return par[x] = getpar(par[x]);
}
long long minimum_walk(std::vector<int> p, int s) {
ll res = 0;
int n = p.size();
fill(par,par+n,-1);
for2(i,0,n) res += abs(i-p[i]);
for2(rep,0,n){
int i = (rep+s)%n;
if(seen[i]) continue;
int l = min(i,p[i]);
int r = max(i,p[i]);
bool SS = 0;
int L = i;
int R = i;
seen[i] = 1;
// cout << rep << endl;
while(1){
int j = -1;
// cout << L << ' ' << R << endl;
if(L != l){
L--;
j = L;
}
if(R != r){
R++;
j = R;
}
if( j == -1) break;
seen[j] = 1;
l = min(l,p[j]);
r = max(r,p[j]);
// cout << j << ' ' << L << ' ' << R << endl;
int x = getpar(i);
int y = getpar(j);
if(x == getpar(s) || y == getpar(s)) SS=1;
if(x != y){
if(par[x] < par[y]) swap(x,y);
par[y] += par[x];
par[x] = y;
}
}
if(SS && rep) res += 2;
}
for2(i,0,n) if(getpar(i) == i) res += 2;
res -= 2;
for(int i = n-1; i > s; i--){
if(par[getpar(i)] != -1) break;
res -= 2;
}
for2(i,0,n){
if(par[getpar(i)] != -1 || i == s) break;
res -= 2;
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
3 ms |
488 KB |
Output is correct |
6 |
Correct |
2 ms |
488 KB |
Output is correct |
7 |
Correct |
4 ms |
488 KB |
Output is correct |
8 |
Correct |
3 ms |
488 KB |
Output is correct |
9 |
Correct |
3 ms |
488 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
680 KB |
Output is correct |
12 |
Correct |
3 ms |
680 KB |
Output is correct |
13 |
Correct |
3 ms |
680 KB |
Output is correct |
14 |
Correct |
3 ms |
680 KB |
Output is correct |
15 |
Correct |
3 ms |
680 KB |
Output is correct |
16 |
Correct |
3 ms |
680 KB |
Output is correct |
17 |
Correct |
2 ms |
680 KB |
Output is correct |
18 |
Correct |
2 ms |
680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
3 ms |
488 KB |
Output is correct |
6 |
Correct |
2 ms |
488 KB |
Output is correct |
7 |
Correct |
4 ms |
488 KB |
Output is correct |
8 |
Correct |
3 ms |
488 KB |
Output is correct |
9 |
Correct |
3 ms |
488 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
680 KB |
Output is correct |
12 |
Correct |
3 ms |
680 KB |
Output is correct |
13 |
Correct |
3 ms |
680 KB |
Output is correct |
14 |
Correct |
3 ms |
680 KB |
Output is correct |
15 |
Correct |
3 ms |
680 KB |
Output is correct |
16 |
Correct |
3 ms |
680 KB |
Output is correct |
17 |
Correct |
2 ms |
680 KB |
Output is correct |
18 |
Correct |
2 ms |
680 KB |
Output is correct |
19 |
Correct |
3 ms |
680 KB |
Output is correct |
20 |
Correct |
2 ms |
680 KB |
Output is correct |
21 |
Correct |
3 ms |
680 KB |
Output is correct |
22 |
Correct |
3 ms |
680 KB |
Output is correct |
23 |
Correct |
2 ms |
680 KB |
Output is correct |
24 |
Correct |
3 ms |
680 KB |
Output is correct |
25 |
Correct |
3 ms |
680 KB |
Output is correct |
26 |
Correct |
2 ms |
680 KB |
Output is correct |
27 |
Correct |
2 ms |
680 KB |
Output is correct |
28 |
Correct |
4 ms |
680 KB |
Output is correct |
29 |
Correct |
2 ms |
680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
3 ms |
488 KB |
Output is correct |
6 |
Correct |
2 ms |
488 KB |
Output is correct |
7 |
Correct |
4 ms |
488 KB |
Output is correct |
8 |
Correct |
3 ms |
488 KB |
Output is correct |
9 |
Correct |
3 ms |
488 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
680 KB |
Output is correct |
12 |
Correct |
3 ms |
680 KB |
Output is correct |
13 |
Correct |
3 ms |
680 KB |
Output is correct |
14 |
Correct |
3 ms |
680 KB |
Output is correct |
15 |
Correct |
3 ms |
680 KB |
Output is correct |
16 |
Correct |
3 ms |
680 KB |
Output is correct |
17 |
Correct |
2 ms |
680 KB |
Output is correct |
18 |
Correct |
2 ms |
680 KB |
Output is correct |
19 |
Correct |
3 ms |
680 KB |
Output is correct |
20 |
Correct |
2 ms |
680 KB |
Output is correct |
21 |
Correct |
3 ms |
680 KB |
Output is correct |
22 |
Correct |
3 ms |
680 KB |
Output is correct |
23 |
Correct |
2 ms |
680 KB |
Output is correct |
24 |
Correct |
3 ms |
680 KB |
Output is correct |
25 |
Correct |
3 ms |
680 KB |
Output is correct |
26 |
Correct |
2 ms |
680 KB |
Output is correct |
27 |
Correct |
2 ms |
680 KB |
Output is correct |
28 |
Correct |
4 ms |
680 KB |
Output is correct |
29 |
Correct |
2 ms |
680 KB |
Output is correct |
30 |
Correct |
244 ms |
13244 KB |
Output is correct |
31 |
Correct |
227 ms |
13292 KB |
Output is correct |
32 |
Correct |
212 ms |
13292 KB |
Output is correct |
33 |
Correct |
175 ms |
13292 KB |
Output is correct |
34 |
Correct |
167 ms |
13292 KB |
Output is correct |
35 |
Correct |
212 ms |
13292 KB |
Output is correct |
36 |
Correct |
182 ms |
13292 KB |
Output is correct |
37 |
Correct |
200 ms |
13304 KB |
Output is correct |
38 |
Correct |
198 ms |
13304 KB |
Output is correct |
39 |
Correct |
200 ms |
13304 KB |
Output is correct |
40 |
Correct |
211 ms |
13304 KB |
Output is correct |
41 |
Correct |
220 ms |
13304 KB |
Output is correct |
42 |
Correct |
274 ms |
13304 KB |
Output is correct |
43 |
Correct |
199 ms |
13304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
13304 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2748' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
3 ms |
488 KB |
Output is correct |
6 |
Correct |
2 ms |
488 KB |
Output is correct |
7 |
Correct |
4 ms |
488 KB |
Output is correct |
8 |
Correct |
3 ms |
488 KB |
Output is correct |
9 |
Correct |
3 ms |
488 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
680 KB |
Output is correct |
12 |
Correct |
3 ms |
680 KB |
Output is correct |
13 |
Correct |
3 ms |
680 KB |
Output is correct |
14 |
Correct |
3 ms |
680 KB |
Output is correct |
15 |
Correct |
3 ms |
680 KB |
Output is correct |
16 |
Correct |
3 ms |
680 KB |
Output is correct |
17 |
Correct |
2 ms |
680 KB |
Output is correct |
18 |
Correct |
2 ms |
680 KB |
Output is correct |
19 |
Correct |
3 ms |
680 KB |
Output is correct |
20 |
Correct |
2 ms |
680 KB |
Output is correct |
21 |
Correct |
3 ms |
680 KB |
Output is correct |
22 |
Correct |
3 ms |
680 KB |
Output is correct |
23 |
Correct |
2 ms |
680 KB |
Output is correct |
24 |
Correct |
3 ms |
680 KB |
Output is correct |
25 |
Correct |
3 ms |
680 KB |
Output is correct |
26 |
Correct |
2 ms |
680 KB |
Output is correct |
27 |
Correct |
2 ms |
680 KB |
Output is correct |
28 |
Correct |
4 ms |
680 KB |
Output is correct |
29 |
Correct |
2 ms |
680 KB |
Output is correct |
30 |
Correct |
244 ms |
13244 KB |
Output is correct |
31 |
Correct |
227 ms |
13292 KB |
Output is correct |
32 |
Correct |
212 ms |
13292 KB |
Output is correct |
33 |
Correct |
175 ms |
13292 KB |
Output is correct |
34 |
Correct |
167 ms |
13292 KB |
Output is correct |
35 |
Correct |
212 ms |
13292 KB |
Output is correct |
36 |
Correct |
182 ms |
13292 KB |
Output is correct |
37 |
Correct |
200 ms |
13304 KB |
Output is correct |
38 |
Correct |
198 ms |
13304 KB |
Output is correct |
39 |
Correct |
200 ms |
13304 KB |
Output is correct |
40 |
Correct |
211 ms |
13304 KB |
Output is correct |
41 |
Correct |
220 ms |
13304 KB |
Output is correct |
42 |
Correct |
274 ms |
13304 KB |
Output is correct |
43 |
Correct |
199 ms |
13304 KB |
Output is correct |
44 |
Incorrect |
2 ms |
13304 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2748' |
45 |
Halted |
0 ms |
0 KB |
- |