#include "books.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 11;
struct range{
int l, r;
bool extend(range rg){
bool changed = false;
if(rg.l < l) changed = true, l = rg.l;
if(rg.r > r) changed = true, r = rg.r;
return changed;
}
friend bool operator!= (const range& s, const range& t){
return s.l != t.l || s.r != t.r;
}
};
bool vis[MAXN];
range cyc[MAXN];
long long minimum_walk(vector<int> p, int s) {
int n = p.size();
bool all_eq = true;
for(int i = 0; i < n; i++) if(p[i] != i) all_eq = false;
if(all_eq) return 0;
int al = 0, ar = n - 1; while(p[al] == al) al++; while(p[ar] == ar) ar--;
long long ans = 0;
for(int i = 0; i < n; i++){
ans += abs(p[i] - i);
}
for(int i = 0; i < n; i++){
if(!vis[i]){
vector<int> c = {i}; int x = p[i]; range r = {i, i};
while(x != c[0]){
r.extend({x, x});
c.push_back(x);
vis[x] = true;
x = p[x];
}
for(auto j : c) cyc[j] = r;
}
}
for(int i = s - 1; i >= 0; i--){
cyc[i].extend(cyc[i + 1]);
}
for(int i = s + 1; i < n; i++){
cyc[i].extend(cyc[i - 1]);
}
{
range cur = cyc[s], nxt = cyc[s];
do{
cur = nxt;
nxt.extend(cyc[nxt.l]);
nxt.extend(cyc[nxt.r]);
}while(cur != nxt);
cyc[s] = nxt;
}
for(int i = s - 1; i >= 0; i--){
cyc[i].extend(cyc[i + 1]);
range cur = cyc[i], nxt = cyc[i];
do{
cur = nxt;
nxt.extend(cyc[nxt.l]);
nxt.extend(cyc[nxt.r]);
}while(cur != nxt);
cyc[i] = nxt;
}
for(int i = s + 1; i < n; i++){
cyc[i].extend(cyc[i - 1]);
range cur = cyc[i], nxt = cyc[i];
do{
cur = nxt;
nxt.extend(cyc[nxt.l]);
nxt.extend(cyc[nxt.r]);
}while(cur != nxt);
cyc[i] = nxt;
}
int c = 0;
while(cyc[c].r < ar){
c = cyc[c].r; ++c; ans += 2;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
312 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
312 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
116 ms |
26844 KB |
Output is correct |
31 |
Correct |
114 ms |
25720 KB |
Output is correct |
32 |
Correct |
75 ms |
14840 KB |
Output is correct |
33 |
Correct |
99 ms |
23668 KB |
Output is correct |
34 |
Correct |
100 ms |
23664 KB |
Output is correct |
35 |
Correct |
105 ms |
23676 KB |
Output is correct |
36 |
Correct |
101 ms |
23612 KB |
Output is correct |
37 |
Correct |
90 ms |
23628 KB |
Output is correct |
38 |
Correct |
89 ms |
23664 KB |
Output is correct |
39 |
Correct |
94 ms |
23680 KB |
Output is correct |
40 |
Correct |
91 ms |
23832 KB |
Output is correct |
41 |
Correct |
101 ms |
24592 KB |
Output is correct |
42 |
Correct |
101 ms |
24632 KB |
Output is correct |
43 |
Correct |
103 ms |
23680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
312 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
116 ms |
26844 KB |
Output is correct |
31 |
Correct |
114 ms |
25720 KB |
Output is correct |
32 |
Correct |
75 ms |
14840 KB |
Output is correct |
33 |
Correct |
99 ms |
23668 KB |
Output is correct |
34 |
Correct |
100 ms |
23664 KB |
Output is correct |
35 |
Correct |
105 ms |
23676 KB |
Output is correct |
36 |
Correct |
101 ms |
23612 KB |
Output is correct |
37 |
Correct |
90 ms |
23628 KB |
Output is correct |
38 |
Correct |
89 ms |
23664 KB |
Output is correct |
39 |
Correct |
94 ms |
23680 KB |
Output is correct |
40 |
Correct |
91 ms |
23832 KB |
Output is correct |
41 |
Correct |
101 ms |
24592 KB |
Output is correct |
42 |
Correct |
101 ms |
24632 KB |
Output is correct |
43 |
Correct |
103 ms |
23680 KB |
Output is correct |
44 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |