#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#include "grader.cpp"
#else
#include "books.h"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 1e6 + 5;
int n, L, R;
int p[SIZE];
int d[SIZE], in[SIZE], out[SIZE];
int add_in[SIZE], add_out[SIZE], add_d[SIZE];
ll minimum_walk(vector<int> p_, int s) {
n = p_.size();
s++;
FOR (i, 1, n) {
p[i] = p_[i - 1] + 1;
in[i] = out[i] = 0;
add_in[i] = add_out[i] = add_d[i] = 0;
}
FOR (i, 1, n) {
if (i < p[i]) {
out[i]++, out[p[i]]--;
in[i + 1]++, in[p[i] + 1]--;
d[i]++, d[p[i]]--;
} else {
out[p[i] + 1]++, out[i + 1]--;
in[p[i]]++, in[i]--;
d[p[i]]++, d[i]--;
}
}
FOR (i, 1, n) {
in[i] += in[i - 1];
out[i] += out[i - 1];
d[i] += d[i - 1];
}
L = 1, R = n;
while (R > s && d[R - 1] == 0) R--;
while (L < s && d[L] == 0) L++;
if (L == R) return 0;
vector<int> v[2];
/* FOR (i, L, R) {
int delta = out[i] - in[i];
while (delta > 0) {
if (v[1].size()) {
int l = v[1].back();
v[1].pop_back();
add_out[l]++, add_out[i]--;
add_in[l + 1]++, add_in[i + 1]--;
add_d[l]++, add_d[i]--;
} else {
v[0].pb(i);
}
delta--;
}
while (delta < 0) {
if (v[0].size()) {
int l = v[0].back();
v[0].pop_back();
add_in[l]++, add_in[i]--;
add_out[l + 1]++, add_out[i + 1]--;
add_d[l]++, add_d[i]--;
} else {
v[1].pb(i);
}
delta++;
}
}
assert(v[0].size() == 0 && v[1].size() == 0);
FOR (i, L, R) {
add_in[i] += add_in[i - 1], in[i] += add_in[i];
add_out[i] += add_out[i - 1], out[i] += add_out[i];
add_d[i] += add_d[i - 1], d[i] += add_d[i];
} */
ll ans = 0;
FOR (i, L, R) {
ans += out[i];
if (i < R && d[i] == 0) ans += 2;
}
return ans;
}
/*
in1
4 0
0 2 3 1
out1
6
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12636 KB |
Output is correct |
5 |
Correct |
1 ms |
12648 KB |
Output is correct |
6 |
Correct |
1 ms |
12632 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12636 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
1 ms |
12632 KB |
Output is correct |
17 |
Correct |
1 ms |
12740 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12636 KB |
Output is correct |
5 |
Correct |
1 ms |
12648 KB |
Output is correct |
6 |
Correct |
1 ms |
12632 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12636 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
1 ms |
12632 KB |
Output is correct |
17 |
Correct |
1 ms |
12740 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
1 ms |
12728 KB |
Output is correct |
20 |
Correct |
1 ms |
12632 KB |
Output is correct |
21 |
Correct |
1 ms |
12636 KB |
Output is correct |
22 |
Correct |
1 ms |
12636 KB |
Output is correct |
23 |
Correct |
1 ms |
12632 KB |
Output is correct |
24 |
Correct |
1 ms |
12636 KB |
Output is correct |
25 |
Correct |
1 ms |
12636 KB |
Output is correct |
26 |
Correct |
1 ms |
12632 KB |
Output is correct |
27 |
Correct |
1 ms |
12636 KB |
Output is correct |
28 |
Correct |
1 ms |
12636 KB |
Output is correct |
29 |
Correct |
1 ms |
12728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12636 KB |
Output is correct |
5 |
Correct |
1 ms |
12648 KB |
Output is correct |
6 |
Correct |
1 ms |
12632 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12636 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
1 ms |
12632 KB |
Output is correct |
17 |
Correct |
1 ms |
12740 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
1 ms |
12728 KB |
Output is correct |
20 |
Correct |
1 ms |
12632 KB |
Output is correct |
21 |
Correct |
1 ms |
12636 KB |
Output is correct |
22 |
Correct |
1 ms |
12636 KB |
Output is correct |
23 |
Correct |
1 ms |
12632 KB |
Output is correct |
24 |
Correct |
1 ms |
12636 KB |
Output is correct |
25 |
Correct |
1 ms |
12636 KB |
Output is correct |
26 |
Correct |
1 ms |
12632 KB |
Output is correct |
27 |
Correct |
1 ms |
12636 KB |
Output is correct |
28 |
Correct |
1 ms |
12636 KB |
Output is correct |
29 |
Correct |
1 ms |
12728 KB |
Output is correct |
30 |
Correct |
123 ms |
41568 KB |
Output is correct |
31 |
Correct |
72 ms |
41420 KB |
Output is correct |
32 |
Correct |
66 ms |
41548 KB |
Output is correct |
33 |
Correct |
66 ms |
41556 KB |
Output is correct |
34 |
Correct |
64 ms |
41496 KB |
Output is correct |
35 |
Correct |
63 ms |
41620 KB |
Output is correct |
36 |
Correct |
62 ms |
41572 KB |
Output is correct |
37 |
Correct |
68 ms |
41592 KB |
Output is correct |
38 |
Correct |
62 ms |
41408 KB |
Output is correct |
39 |
Correct |
62 ms |
41548 KB |
Output is correct |
40 |
Correct |
71 ms |
41376 KB |
Output is correct |
41 |
Correct |
70 ms |
41432 KB |
Output is correct |
42 |
Correct |
82 ms |
41552 KB |
Output is correct |
43 |
Correct |
70 ms |
41520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
12632 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 |
1 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12636 KB |
Output is correct |
5 |
Correct |
1 ms |
12648 KB |
Output is correct |
6 |
Correct |
1 ms |
12632 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12636 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
1 ms |
12632 KB |
Output is correct |
17 |
Correct |
1 ms |
12740 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
1 ms |
12728 KB |
Output is correct |
20 |
Correct |
1 ms |
12632 KB |
Output is correct |
21 |
Correct |
1 ms |
12636 KB |
Output is correct |
22 |
Correct |
1 ms |
12636 KB |
Output is correct |
23 |
Correct |
1 ms |
12632 KB |
Output is correct |
24 |
Correct |
1 ms |
12636 KB |
Output is correct |
25 |
Correct |
1 ms |
12636 KB |
Output is correct |
26 |
Correct |
1 ms |
12632 KB |
Output is correct |
27 |
Correct |
1 ms |
12636 KB |
Output is correct |
28 |
Correct |
1 ms |
12636 KB |
Output is correct |
29 |
Correct |
1 ms |
12728 KB |
Output is correct |
30 |
Correct |
123 ms |
41568 KB |
Output is correct |
31 |
Correct |
72 ms |
41420 KB |
Output is correct |
32 |
Correct |
66 ms |
41548 KB |
Output is correct |
33 |
Correct |
66 ms |
41556 KB |
Output is correct |
34 |
Correct |
64 ms |
41496 KB |
Output is correct |
35 |
Correct |
63 ms |
41620 KB |
Output is correct |
36 |
Correct |
62 ms |
41572 KB |
Output is correct |
37 |
Correct |
68 ms |
41592 KB |
Output is correct |
38 |
Correct |
62 ms |
41408 KB |
Output is correct |
39 |
Correct |
62 ms |
41548 KB |
Output is correct |
40 |
Correct |
71 ms |
41376 KB |
Output is correct |
41 |
Correct |
70 ms |
41432 KB |
Output is correct |
42 |
Correct |
82 ms |
41552 KB |
Output is correct |
43 |
Correct |
70 ms |
41520 KB |
Output is correct |
44 |
Incorrect |
1 ms |
12632 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |