#include <bits/stdc++.h>
#include "books.h"
using namespace std;
#define Fi first
#define Se second
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define szz(x) (int)(x).size()
#define rep(i, n) for(int i=0;i<(n);i++)
typedef tuple<int, int, int> t3;
int c[1000010], lv[1000010], rv[1000010], T[1000010], S[1000010];
ll minimum_walk(vector<int> p, int s) {
int n = szz(p);
ll ans = 0;
rep(i, n) ans += abs(i - p[i]);
auto upd = [&](int s, int e) {
for(int i=s;i<=e;i++) {
T[i] += (i > s ? T[i-1] : 0);
S[i] = (i > s ? S[i-1] : 0) + (T[i] == 0);
}
};
auto clr = [&](int s, int e) {
for(int i=s;i<=e;i++) T[i] = S[i] = 0;
};
auto gs = [&](int s, int e) {
return (e ? S[e-1] : 0) - (s ? S[s-1] : 0);
};
rep(i, n) if(!c[i]) {
int l = i, r = i;
for(int j=p[i];j!=i;j=p[j]) l = min(l, j), r = max(r, j);
c[i] = 1, lv[i] = l, rv[i] = r;
for(int j=p[i];j!=i;j=p[j]) c[j] = 1, lv[j] = l, rv[j] = r;
}
memset(c, 0, sizeof c);
int l = s, r = s;
auto expend = [&]() {
for(int x=r+1;x<n;x++) {
if(lv[x] < l) {
int r2 = x, l2 = -1;
for(int i=p[r2];i!=r2;i=p[i]) {
if(i < l) l2 = max(l2, i);
}
return pii(l2, r2);
}
}
return pii(-1, -1);
};
vector <int> q; q.pb(s);
auto push = [&](int x) {
if(l > x) {
for(int a=l-1;a>=x;a--) q.pb(a);
l = x;
}
if(r < x) {
for(int a=r+1;a<=x;a++) q.pb(a);
r = x;
}
};
while(1) {
rep(i, szz(q)) {
int t = q[i];
if(c[t]) continue;
c[t] = 1;
for(int x=p[t];x!=t;x=p[x]) {
push(x);
c[x] = 1;
}
}
auto [l2, r2] = expend();
if(l2 == -1) break;
q.clear();
for(int i=l2+1;i<r2;i++) T[min(i, p[i])]++, T[max(i, p[i])]--;
upd(l2, r2);
ans += 2 * min(gs(l2, l), gs(r, r2));
clr(l2, r2);
push(l2);
push(r2);
}
int L = 0, R = n - 1;
while(L < l && p[L] == L) ++L;
while(r < R && p[R] == R) --R;
rep(i, n) T[min(i, p[i])]++, T[max(i, p[i])]--;
upd(0, n - 1);
ans += 2 * (gs(L, l) + gs(r, R));
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4204 KB |
Output is correct |
2 |
Correct |
3 ms |
4204 KB |
Output is correct |
3 |
Correct |
3 ms |
4204 KB |
Output is correct |
4 |
Correct |
3 ms |
4204 KB |
Output is correct |
5 |
Correct |
3 ms |
4204 KB |
Output is correct |
6 |
Correct |
3 ms |
4204 KB |
Output is correct |
7 |
Correct |
3 ms |
4204 KB |
Output is correct |
8 |
Correct |
3 ms |
4204 KB |
Output is correct |
9 |
Correct |
3 ms |
4204 KB |
Output is correct |
10 |
Correct |
3 ms |
4204 KB |
Output is correct |
11 |
Correct |
3 ms |
4204 KB |
Output is correct |
12 |
Correct |
3 ms |
4204 KB |
Output is correct |
13 |
Correct |
3 ms |
4204 KB |
Output is correct |
14 |
Correct |
3 ms |
4204 KB |
Output is correct |
15 |
Correct |
3 ms |
4204 KB |
Output is correct |
16 |
Correct |
3 ms |
4332 KB |
Output is correct |
17 |
Correct |
3 ms |
4204 KB |
Output is correct |
18 |
Correct |
3 ms |
4224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4204 KB |
Output is correct |
2 |
Correct |
3 ms |
4204 KB |
Output is correct |
3 |
Correct |
3 ms |
4204 KB |
Output is correct |
4 |
Correct |
3 ms |
4204 KB |
Output is correct |
5 |
Correct |
3 ms |
4204 KB |
Output is correct |
6 |
Correct |
3 ms |
4204 KB |
Output is correct |
7 |
Correct |
3 ms |
4204 KB |
Output is correct |
8 |
Correct |
3 ms |
4204 KB |
Output is correct |
9 |
Correct |
3 ms |
4204 KB |
Output is correct |
10 |
Correct |
3 ms |
4204 KB |
Output is correct |
11 |
Correct |
3 ms |
4204 KB |
Output is correct |
12 |
Correct |
3 ms |
4204 KB |
Output is correct |
13 |
Correct |
3 ms |
4204 KB |
Output is correct |
14 |
Correct |
3 ms |
4204 KB |
Output is correct |
15 |
Correct |
3 ms |
4204 KB |
Output is correct |
16 |
Correct |
3 ms |
4332 KB |
Output is correct |
17 |
Correct |
3 ms |
4204 KB |
Output is correct |
18 |
Correct |
3 ms |
4224 KB |
Output is correct |
19 |
Correct |
3 ms |
4332 KB |
Output is correct |
20 |
Correct |
3 ms |
4332 KB |
Output is correct |
21 |
Correct |
3 ms |
4332 KB |
Output is correct |
22 |
Correct |
3 ms |
4352 KB |
Output is correct |
23 |
Correct |
3 ms |
4332 KB |
Output is correct |
24 |
Correct |
3 ms |
4332 KB |
Output is correct |
25 |
Correct |
3 ms |
4332 KB |
Output is correct |
26 |
Correct |
3 ms |
4332 KB |
Output is correct |
27 |
Correct |
3 ms |
4332 KB |
Output is correct |
28 |
Correct |
3 ms |
4332 KB |
Output is correct |
29 |
Correct |
3 ms |
4332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4204 KB |
Output is correct |
2 |
Correct |
3 ms |
4204 KB |
Output is correct |
3 |
Correct |
3 ms |
4204 KB |
Output is correct |
4 |
Correct |
3 ms |
4204 KB |
Output is correct |
5 |
Correct |
3 ms |
4204 KB |
Output is correct |
6 |
Correct |
3 ms |
4204 KB |
Output is correct |
7 |
Correct |
3 ms |
4204 KB |
Output is correct |
8 |
Correct |
3 ms |
4204 KB |
Output is correct |
9 |
Correct |
3 ms |
4204 KB |
Output is correct |
10 |
Correct |
3 ms |
4204 KB |
Output is correct |
11 |
Correct |
3 ms |
4204 KB |
Output is correct |
12 |
Correct |
3 ms |
4204 KB |
Output is correct |
13 |
Correct |
3 ms |
4204 KB |
Output is correct |
14 |
Correct |
3 ms |
4204 KB |
Output is correct |
15 |
Correct |
3 ms |
4204 KB |
Output is correct |
16 |
Correct |
3 ms |
4332 KB |
Output is correct |
17 |
Correct |
3 ms |
4204 KB |
Output is correct |
18 |
Correct |
3 ms |
4224 KB |
Output is correct |
19 |
Correct |
3 ms |
4332 KB |
Output is correct |
20 |
Correct |
3 ms |
4332 KB |
Output is correct |
21 |
Correct |
3 ms |
4332 KB |
Output is correct |
22 |
Correct |
3 ms |
4352 KB |
Output is correct |
23 |
Correct |
3 ms |
4332 KB |
Output is correct |
24 |
Correct |
3 ms |
4332 KB |
Output is correct |
25 |
Correct |
3 ms |
4332 KB |
Output is correct |
26 |
Correct |
3 ms |
4332 KB |
Output is correct |
27 |
Correct |
3 ms |
4332 KB |
Output is correct |
28 |
Correct |
3 ms |
4332 KB |
Output is correct |
29 |
Correct |
3 ms |
4332 KB |
Output is correct |
30 |
Correct |
374 ms |
32340 KB |
Output is correct |
31 |
Correct |
381 ms |
32212 KB |
Output is correct |
32 |
Correct |
142 ms |
28268 KB |
Output is correct |
33 |
Correct |
148 ms |
28268 KB |
Output is correct |
34 |
Correct |
149 ms |
28268 KB |
Output is correct |
35 |
Correct |
151 ms |
28268 KB |
Output is correct |
36 |
Correct |
148 ms |
28268 KB |
Output is correct |
37 |
Correct |
152 ms |
28268 KB |
Output is correct |
38 |
Correct |
148 ms |
28268 KB |
Output is correct |
39 |
Correct |
149 ms |
28268 KB |
Output is correct |
40 |
Correct |
160 ms |
28396 KB |
Output is correct |
41 |
Correct |
208 ms |
29408 KB |
Output is correct |
42 |
Correct |
188 ms |
28268 KB |
Output is correct |
43 |
Correct |
165 ms |
32212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4332 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4332 KB |
3rd lines differ - on the 1st token, expected: '9800', found: '9802' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4204 KB |
Output is correct |
2 |
Correct |
3 ms |
4204 KB |
Output is correct |
3 |
Correct |
3 ms |
4204 KB |
Output is correct |
4 |
Correct |
3 ms |
4204 KB |
Output is correct |
5 |
Correct |
3 ms |
4204 KB |
Output is correct |
6 |
Correct |
3 ms |
4204 KB |
Output is correct |
7 |
Correct |
3 ms |
4204 KB |
Output is correct |
8 |
Correct |
3 ms |
4204 KB |
Output is correct |
9 |
Correct |
3 ms |
4204 KB |
Output is correct |
10 |
Correct |
3 ms |
4204 KB |
Output is correct |
11 |
Correct |
3 ms |
4204 KB |
Output is correct |
12 |
Correct |
3 ms |
4204 KB |
Output is correct |
13 |
Correct |
3 ms |
4204 KB |
Output is correct |
14 |
Correct |
3 ms |
4204 KB |
Output is correct |
15 |
Correct |
3 ms |
4204 KB |
Output is correct |
16 |
Correct |
3 ms |
4332 KB |
Output is correct |
17 |
Correct |
3 ms |
4204 KB |
Output is correct |
18 |
Correct |
3 ms |
4224 KB |
Output is correct |
19 |
Correct |
3 ms |
4332 KB |
Output is correct |
20 |
Correct |
3 ms |
4332 KB |
Output is correct |
21 |
Correct |
3 ms |
4332 KB |
Output is correct |
22 |
Correct |
3 ms |
4352 KB |
Output is correct |
23 |
Correct |
3 ms |
4332 KB |
Output is correct |
24 |
Correct |
3 ms |
4332 KB |
Output is correct |
25 |
Correct |
3 ms |
4332 KB |
Output is correct |
26 |
Correct |
3 ms |
4332 KB |
Output is correct |
27 |
Correct |
3 ms |
4332 KB |
Output is correct |
28 |
Correct |
3 ms |
4332 KB |
Output is correct |
29 |
Correct |
3 ms |
4332 KB |
Output is correct |
30 |
Correct |
374 ms |
32340 KB |
Output is correct |
31 |
Correct |
381 ms |
32212 KB |
Output is correct |
32 |
Correct |
142 ms |
28268 KB |
Output is correct |
33 |
Correct |
148 ms |
28268 KB |
Output is correct |
34 |
Correct |
149 ms |
28268 KB |
Output is correct |
35 |
Correct |
151 ms |
28268 KB |
Output is correct |
36 |
Correct |
148 ms |
28268 KB |
Output is correct |
37 |
Correct |
152 ms |
28268 KB |
Output is correct |
38 |
Correct |
148 ms |
28268 KB |
Output is correct |
39 |
Correct |
149 ms |
28268 KB |
Output is correct |
40 |
Correct |
160 ms |
28396 KB |
Output is correct |
41 |
Correct |
208 ms |
29408 KB |
Output is correct |
42 |
Correct |
188 ms |
28268 KB |
Output is correct |
43 |
Correct |
165 ms |
32212 KB |
Output is correct |
44 |
Correct |
3 ms |
4332 KB |
Output is correct |
45 |
Incorrect |
3 ms |
4332 KB |
3rd lines differ - on the 1st token, expected: '9800', found: '9802' |
46 |
Halted |
0 ms |
0 KB |
- |