#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<ld> point;
void debug_out(){cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
cerr << H << ' ';
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", debug_out(__VA_ARGS__)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
const int maxn = 1e6 + 10;
int n, a[maxn], L[maxn], R[maxn];
bool vis[maxn];
ll solve(int l, int r, int tl, int tr){
int ql = min(l, L[l]);
int qr = max(r, R[r]);
while(ql < l || qr > r){
while(ql < l){
l--;
ql = min(ql, L[l]);
qr = max(qr, R[l]);
}
while(qr > r){
r++;
ql = min(ql, L[r]);
qr = max(qr, R[r]);
}
}
//debug(l, r);
ll ans = 0;
while(l != tl || r != tr){
int cntl = 0;
bool markl = false;
int idkl = l, idkr = r;
int ql = l, qr = r;
while(idkl > tl && qr == r){
idkl--;
cntl++;
ql = min(ql, L[idkl]);
qr = max(qr, R[idkl]);
// debug(idkl, idkr, 1);
while(ql < idkl || qr > idkr){
while(ql < idkl){
idkl--;
ql = min(ql, L[idkl]);
qr = max(qr, R[idkl]);
}
while(idkr < qr){
idkr++;
ql = min(ql, L[idkr]);
qr = max(qr, R[idkr]);
}
// debug(idkl, idkr);
}
}
if (qr != r) markl = true;
// debug(1);
int cntr = 0;
bool markr = false;
idkl = l, idkr = r;
int ql2 = l, qr2 = r;
// debug(idkr, tr);
while(idkr < tr && ql2 == l){
idkr++;
cntr++;
ql2 = min(ql2, L[idkr]);
qr2 = max(qr2, R[idkr]);
// debug(idkl, idkr, 1);
while(ql2 < idkl || qr2 > idkr){
while(ql2 < idkl){
idkl--;
ql2 = min(ql2, L[idkl]);
qr2 = max(qr2, R[idkl]);
}
while(idkr < qr2){
idkr++;
ql2 = min(ql2, L[idkr]);
qr2 = max(qr2, R[idkr]);
}
// debug(idkl, idkr);
}
}
if (ql2 != l) markr = true;
// debug(markl, markr, cntl, cntr);
if (markl && markr){
if (cntl < cntr){
l = ql;
r = qr;
ans += 2*cntl;
}
else{
l = ql2;
r = qr2;
ans += 2*cntr;
}
}
else{
l = tl;
r = tr;
ans += 2*cntl + 2*cntr;
}
}
return ans;
}
ll minimum_walk(vector<int> p, int s) {
n = p.size();
int mn = s, mx = s;
ll ans = 0;
for (int i = 0; i < n; i++){
a[i] = p[i];
if (a[i] != i){
mn = min(mn, i);
mx = max(mx, i);
}
ans += abs(a[i] - i);
}
for (int i = 0; i < n; i++){
vector<int> tmp;
int x = i;
while(!vis[x]){
vis[x] = true;
tmp.push_back(x);
x = a[x];
}
// debug(i);
//for (auto x: tmp) debug(x);
sort(all(tmp));
for (auto x: tmp){
L[x] = tmp[0];
R[x] = tmp.back();
// debug(x, L[x], R[x]);
}
}
return ans + solve(s, s, mn, mx);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
340 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 |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
300 KB |
Output is correct |
12 |
Correct |
0 ms |
316 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 |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
340 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 |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
300 KB |
Output is correct |
12 |
Correct |
0 ms |
316 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 |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
308 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
308 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
340 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 |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
300 KB |
Output is correct |
12 |
Correct |
0 ms |
316 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 |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
308 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
308 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
173 ms |
30672 KB |
Output is correct |
31 |
Correct |
170 ms |
29624 KB |
Output is correct |
32 |
Correct |
106 ms |
27488 KB |
Output is correct |
33 |
Correct |
116 ms |
27468 KB |
Output is correct |
34 |
Correct |
110 ms |
27448 KB |
Output is correct |
35 |
Correct |
120 ms |
27488 KB |
Output is correct |
36 |
Correct |
109 ms |
27496 KB |
Output is correct |
37 |
Correct |
110 ms |
27584 KB |
Output is correct |
38 |
Correct |
121 ms |
27620 KB |
Output is correct |
39 |
Correct |
125 ms |
27596 KB |
Output is correct |
40 |
Correct |
137 ms |
27772 KB |
Output is correct |
41 |
Correct |
160 ms |
28488 KB |
Output is correct |
42 |
Correct |
151 ms |
28448 KB |
Output is correct |
43 |
Correct |
110 ms |
27500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
340 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 |
340 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 |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
340 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 |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
300 KB |
Output is correct |
12 |
Correct |
0 ms |
316 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 |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
308 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
308 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
173 ms |
30672 KB |
Output is correct |
31 |
Correct |
170 ms |
29624 KB |
Output is correct |
32 |
Correct |
106 ms |
27488 KB |
Output is correct |
33 |
Correct |
116 ms |
27468 KB |
Output is correct |
34 |
Correct |
110 ms |
27448 KB |
Output is correct |
35 |
Correct |
120 ms |
27488 KB |
Output is correct |
36 |
Correct |
109 ms |
27496 KB |
Output is correct |
37 |
Correct |
110 ms |
27584 KB |
Output is correct |
38 |
Correct |
121 ms |
27620 KB |
Output is correct |
39 |
Correct |
125 ms |
27596 KB |
Output is correct |
40 |
Correct |
137 ms |
27772 KB |
Output is correct |
41 |
Correct |
160 ms |
28488 KB |
Output is correct |
42 |
Correct |
151 ms |
28448 KB |
Output is correct |
43 |
Correct |
110 ms |
27500 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
312 KB |
Output is correct |
47 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
304 KB |
Output is correct |
54 |
Correct |
1 ms |
340 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 |
340 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 |
312 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
112 ms |
27632 KB |
Output is correct |
65 |
Correct |
103 ms |
27496 KB |
Output is correct |
66 |
Correct |
114 ms |
27500 KB |
Output is correct |
67 |
Correct |
115 ms |
27520 KB |
Output is correct |
68 |
Correct |
11 ms |
2900 KB |
Output is correct |
69 |
Correct |
11 ms |
2900 KB |
Output is correct |
70 |
Correct |
10 ms |
2980 KB |
Output is correct |
71 |
Correct |
14 ms |
2944 KB |
Output is correct |
72 |
Correct |
10 ms |
2900 KB |
Output is correct |
73 |
Correct |
13 ms |
2948 KB |
Output is correct |
74 |
Correct |
108 ms |
27508 KB |
Output is correct |
75 |
Correct |
108 ms |
27500 KB |
Output is correct |
76 |
Correct |
107 ms |
27504 KB |
Output is correct |
77 |
Correct |
112 ms |
27628 KB |
Output is correct |
78 |
Correct |
106 ms |
27504 KB |
Output is correct |
79 |
Correct |
106 ms |
27500 KB |
Output is correct |
80 |
Correct |
96 ms |
27504 KB |
Output is correct |
81 |
Correct |
99 ms |
27500 KB |
Output is correct |
82 |
Correct |
98 ms |
27508 KB |
Output is correct |
83 |
Correct |
110 ms |
27508 KB |
Output is correct |
84 |
Correct |
111 ms |
27504 KB |
Output is correct |
85 |
Correct |
114 ms |
27500 KB |
Output is correct |
86 |
Correct |
107 ms |
27468 KB |
Output is correct |
87 |
Correct |
107 ms |
27508 KB |
Output is correct |
88 |
Correct |
109 ms |
27500 KB |
Output is correct |
89 |
Correct |
111 ms |
27504 KB |
Output is correct |
90 |
Correct |
119 ms |
27632 KB |
Output is correct |
91 |
Correct |
117 ms |
27504 KB |
Output is correct |
92 |
Correct |
137 ms |
27636 KB |
Output is correct |