#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 range operator+ (const range& s, const range& t){
return {min(s.l, t.l), max(s.r, t.r)};
}
friend void operator+= (range& s, const range& t){
s = s + t;
}
friend bool operator!= (const range& s, const range& t){
return s.l != t.l || s.r != t.r;
}
};
bool vis[MAXN];
range cyc[MAXN];
int dp[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;
}
range cur = cyc[s]; for(int j = cur.l; j <= cur.r; j++) dp[j] = 0;
int res = 1;
while(cur.l > al || cur.r < ar){
range nxt {s, s};
if(cur.l > al) nxt += cyc[cur.l - 1] + cyc[cur.r];
if(cur.r < ar) nxt += cyc[cur.l] + cyc[cur.r + 1];
for(int j = nxt.l; j < cur.l; j++) dp[j] = res;
for(int j = nxt.r; j > cur.r; j--) dp[j] = res;
cur = nxt;
++res;
}
int a1 = dp[al], a2 = dp[ar];
{
int c = al;
while(cyc[c].r < ar){
c = cyc[c].r; ++c; a1++;
}
}
{
int c = ar;
while(cyc[c].l > al){
c = cyc[c].l; --c; a2++;
}
}
ans += min(a1, a2) * 2;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
224 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
224 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
111 ms |
22648 KB |
Output is correct |
31 |
Correct |
114 ms |
22780 KB |
Output is correct |
32 |
Correct |
72 ms |
8048 KB |
Output is correct |
33 |
Correct |
112 ms |
20824 KB |
Output is correct |
34 |
Correct |
108 ms |
20780 KB |
Output is correct |
35 |
Correct |
111 ms |
20724 KB |
Output is correct |
36 |
Correct |
104 ms |
20720 KB |
Output is correct |
37 |
Correct |
92 ms |
20732 KB |
Output is correct |
38 |
Correct |
91 ms |
20856 KB |
Output is correct |
39 |
Correct |
90 ms |
20856 KB |
Output is correct |
40 |
Correct |
109 ms |
20980 KB |
Output is correct |
41 |
Correct |
106 ms |
21660 KB |
Output is correct |
42 |
Correct |
101 ms |
21812 KB |
Output is correct |
43 |
Correct |
101 ms |
20744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
224 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
111 ms |
22648 KB |
Output is correct |
31 |
Correct |
114 ms |
22780 KB |
Output is correct |
32 |
Correct |
72 ms |
8048 KB |
Output is correct |
33 |
Correct |
112 ms |
20824 KB |
Output is correct |
34 |
Correct |
108 ms |
20780 KB |
Output is correct |
35 |
Correct |
111 ms |
20724 KB |
Output is correct |
36 |
Correct |
104 ms |
20720 KB |
Output is correct |
37 |
Correct |
92 ms |
20732 KB |
Output is correct |
38 |
Correct |
91 ms |
20856 KB |
Output is correct |
39 |
Correct |
90 ms |
20856 KB |
Output is correct |
40 |
Correct |
109 ms |
20980 KB |
Output is correct |
41 |
Correct |
106 ms |
21660 KB |
Output is correct |
42 |
Correct |
101 ms |
21812 KB |
Output is correct |
43 |
Correct |
101 ms |
20744 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
256 KB |
Output is correct |
54 |
Correct |
1 ms |
304 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
300 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
102 ms |
27532 KB |
Output is correct |
65 |
Correct |
102 ms |
27504 KB |
Output is correct |
66 |
Correct |
110 ms |
27564 KB |
Output is correct |
67 |
Correct |
96 ms |
27524 KB |
Output is correct |
68 |
Correct |
16 ms |
2872 KB |
Output is correct |
69 |
Correct |
10 ms |
2868 KB |
Output is correct |
70 |
Correct |
10 ms |
2900 KB |
Output is correct |
71 |
Correct |
10 ms |
2864 KB |
Output is correct |
72 |
Correct |
10 ms |
2876 KB |
Output is correct |
73 |
Correct |
10 ms |
3000 KB |
Output is correct |
74 |
Correct |
108 ms |
27576 KB |
Output is correct |
75 |
Correct |
109 ms |
27496 KB |
Output is correct |
76 |
Correct |
106 ms |
27508 KB |
Output is correct |
77 |
Correct |
105 ms |
27500 KB |
Output is correct |
78 |
Correct |
107 ms |
27500 KB |
Output is correct |
79 |
Correct |
107 ms |
27468 KB |
Output is correct |
80 |
Correct |
80 ms |
14952 KB |
Output is correct |
81 |
Correct |
108 ms |
27212 KB |
Output is correct |
82 |
Correct |
101 ms |
27248 KB |
Output is correct |
83 |
Correct |
112 ms |
27524 KB |
Output is correct |
84 |
Correct |
110 ms |
27504 KB |
Output is correct |
85 |
Correct |
107 ms |
27760 KB |
Output is correct |
86 |
Correct |
96 ms |
27504 KB |
Output is correct |
87 |
Correct |
105 ms |
27000 KB |
Output is correct |
88 |
Correct |
107 ms |
27508 KB |
Output is correct |
89 |
Correct |
111 ms |
27684 KB |
Output is correct |
90 |
Correct |
94 ms |
27508 KB |
Output is correct |
91 |
Correct |
94 ms |
27496 KB |
Output is correct |
92 |
Correct |
94 ms |
27620 KB |
Output is correct |