#include <bits/stdc++.h>
using namespace std;
#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif
#define int long long
const int N = (int) 2e5 + 5;
const int LOG = 18;
int n;
int p[N], lg[N];
int st[LOG][N];
int cmb(int a, int b) {
return p[a] > p[b] ? a : b;
}
int get(int l, int r) {
int k = lg[r - l + 1];
return cmb(st[k][l], st[k][r - (1 << k) + 1]);
}
int dnc(int l, int r) {
if (l == r) {
return 0;
}
int mid = get(l, r);
if (l == mid) {
return abs(mid - get(l + 1, r)) + dnc(l + 1, r);
}
if (mid == r) {
return abs(mid - get(l, r - 1)) + dnc(l, r - 1);
}
return max(abs(mid - get(mid + 1, r)) + dnc(mid + 1, r), abs(mid - get(l, mid - 1)) + dnc(l, mid - 1));
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef ngu
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
#endif
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
if (i > 1) {
lg[i] = lg[i / 2] + 1;
}
cin >> p[i];
}
iota(st[0] + 1, st[0] + n + 1, 1);
assert(lg[n] < LOG);
for (int j = 1; j <= lg[n]; ++j) {
for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
st[j][i] = cmb(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}
}
cout << dnc(1, n);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
4672 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
4672 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
16732 KB |
Output is correct |
12 |
Correct |
2 ms |
16732 KB |
Output is correct |
13 |
Correct |
2 ms |
16728 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
16732 KB |
Output is correct |
16 |
Correct |
2 ms |
16732 KB |
Output is correct |
17 |
Correct |
2 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
4672 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
16732 KB |
Output is correct |
12 |
Correct |
2 ms |
16732 KB |
Output is correct |
13 |
Correct |
2 ms |
16728 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
16732 KB |
Output is correct |
16 |
Correct |
2 ms |
16732 KB |
Output is correct |
17 |
Correct |
2 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
22872 KB |
Output is correct |
19 |
Correct |
3 ms |
23128 KB |
Output is correct |
20 |
Correct |
3 ms |
23132 KB |
Output is correct |
21 |
Correct |
4 ms |
22876 KB |
Output is correct |
22 |
Correct |
3 ms |
22872 KB |
Output is correct |
23 |
Correct |
3 ms |
22876 KB |
Output is correct |
24 |
Correct |
3 ms |
22872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
4672 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
16732 KB |
Output is correct |
12 |
Correct |
2 ms |
16732 KB |
Output is correct |
13 |
Correct |
2 ms |
16728 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
16732 KB |
Output is correct |
16 |
Correct |
2 ms |
16732 KB |
Output is correct |
17 |
Correct |
2 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
22872 KB |
Output is correct |
19 |
Correct |
3 ms |
23128 KB |
Output is correct |
20 |
Correct |
3 ms |
23132 KB |
Output is correct |
21 |
Correct |
4 ms |
22876 KB |
Output is correct |
22 |
Correct |
3 ms |
22872 KB |
Output is correct |
23 |
Correct |
3 ms |
22876 KB |
Output is correct |
24 |
Correct |
3 ms |
22872 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Incorrect |
3 ms |
22876 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
4672 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
16732 KB |
Output is correct |
12 |
Correct |
2 ms |
16732 KB |
Output is correct |
13 |
Correct |
2 ms |
16728 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
16732 KB |
Output is correct |
16 |
Correct |
2 ms |
16732 KB |
Output is correct |
17 |
Correct |
2 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
22872 KB |
Output is correct |
19 |
Correct |
3 ms |
23128 KB |
Output is correct |
20 |
Correct |
3 ms |
23132 KB |
Output is correct |
21 |
Correct |
4 ms |
22876 KB |
Output is correct |
22 |
Correct |
3 ms |
22872 KB |
Output is correct |
23 |
Correct |
3 ms |
22876 KB |
Output is correct |
24 |
Correct |
3 ms |
22872 KB |
Output is correct |
25 |
Correct |
20 ms |
30556 KB |
Output is correct |
26 |
Correct |
22 ms |
32764 KB |
Output is correct |
27 |
Correct |
24 ms |
33088 KB |
Output is correct |
28 |
Correct |
23 ms |
31984 KB |
Output is correct |
29 |
Correct |
25 ms |
32060 KB |
Output is correct |
30 |
Correct |
23 ms |
32080 KB |
Output is correct |
31 |
Correct |
25 ms |
32092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
2 ms |
14684 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
4672 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
16732 KB |
Output is correct |
12 |
Correct |
2 ms |
16732 KB |
Output is correct |
13 |
Correct |
2 ms |
16728 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
16732 KB |
Output is correct |
16 |
Correct |
2 ms |
16732 KB |
Output is correct |
17 |
Correct |
2 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
22872 KB |
Output is correct |
19 |
Correct |
3 ms |
23128 KB |
Output is correct |
20 |
Correct |
3 ms |
23132 KB |
Output is correct |
21 |
Correct |
4 ms |
22876 KB |
Output is correct |
22 |
Correct |
3 ms |
22872 KB |
Output is correct |
23 |
Correct |
3 ms |
22876 KB |
Output is correct |
24 |
Correct |
3 ms |
22872 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Incorrect |
3 ms |
22876 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |