CF 1407D Discrete Centrifugal Jumps

题目描述

https://codeforces.com/contest/1407/problem/D

Solution

注意到对于 $f[i]$ 能够转移的 $f[j]$ 一定是 $pre[i - 1]$ 后缀最小或最大值

这个东西可以拿单调栈维护,然后稍微注意下细节即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstdio>
#include <stack>
#define maxn 300010
#define INF 1000000000
using namespace std;

int n, a[maxn];

stack<int> S1, S2;

int f[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
f[1] = 0; S1.push(1); S2.push(1);
for (int i = 2; i <= n; ++i) {
f[i] = f[i - 1] + 1; bool first = 1;
while (!S1.empty() && a[i] >= a[S1.top()]) {
if (a[i] != a[S1.top()]) f[i] = min(f[i], f[S1.top()] + 1);
else if (first) f[i] = min(f[i], f[S1.top()] + 1), first = 0;
S1.pop();
}
if (!S1.empty() && first) f[i] = min(f[i], f[S1.top()] + 1);
S1.push(i); first = 1;
while (!S2.empty() && a[i] <= a[S2.top()]) {
if (a[i] != a[S2.top()]) f[i] = min(f[i], f[S2.top()] + 1);
else if (first) f[i] = min(f[i], f[S2.top()] + 1), first = 0;
S2.pop();
}
if (!S2.empty() && first) f[i] = min(f[i], f[S2.top()] + 1);
S2.push(i);
} cout << f[n] << "\n";
return 0;
}

注意到其实我们还有另一种做法,如果我们没有发现能够转移的 $j$ 满足后缀最小最大值的话

我们考虑随着 $i$ 的增大,能够转移的 $j$ 是逐渐减少的,那么我们可以用线段树每次把不能转移的 $j$ 给干掉

注意到一个 $j$ 只需要干掉一次,所以我们直接维护区间最大最小值,如果需要干掉就暴力干掉,这样复杂度仍然是 $O(n\log n)$

所以总的复杂度就是 $O(n\log n)$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <stack>
#include <queue>
#define ll long long
#define maxn 300010
#define maxm 100010
#define cn const node
#define cQ const queue
#define pb push_back
#define INF 1000000000
#define lowbit(i) ((i) & (-i))
using namespace std;

int n, a[maxn];

int lb[maxn], ls[maxn];
stack<int> S;


#define lc i << 1
#define rc i << 1 | 1
struct Seg {
int V1, V2, v1, v2, tag1, tag2;
} T[maxn * 4];
inline void maintain(int i) {
T[i].V1 = min(T[lc].V1, T[rc].V1);
T[i].V2 = min(T[lc].V2, T[rc].V2);
T[i].v1 = min(T[lc].v1, T[rc].v1);
T[i].v2 = max(T[lc].v2, T[rc].v2);
}

void update1(int i, int l, int r, int L, int R, int v) {
if (l > R || r < L || T[i].v1 > v) return ;
if (l == r) return (void) (T[i].v1 = INF, T[i].V1 = n + 1);
int m = l + r >> 1;
update1(lc, l, m, L, R, v); update1(rc, m + 1, r, L, R, v);
maintain(i);
}

void update2(int i, int l, int r, int L, int R, int v) {
if (l > R || r < L || T[i].v2 < v) return ;
if (l == r) return (void) (T[i].v2 = 0, T[i].V2 = n + 1);
int m = l + r >> 1;
update2(lc, l, m, L, R, v); update2(rc, m + 1, r, L, R, v);
maintain(i);
}

void update(int i, int l, int r, int k, int v) {
if (l == r) return (void) (T[i].V1 = T[i].V2 = v, T[i].v1 = T[i].v2 = a[l]);
int m = l + r >> 1;
if (k <= m) update(lc, l, m, k, v);
else update(rc, m + 1, r, k, v);
maintain(i);
}

int query1(int i, int l, int r, int L, int R) {
if (l > R || r < L) return n + 1;
if (L <= l && r <= R) return T[i].V1;
int m = l + r >> 1;
return min(query1(lc, l, m, L, R), query1(rc, m + 1, r, L, R));
}

int query2(int i, int l, int r, int L, int R) {
if (l > R || r < L) return n + 1;
if (L <= l && r <= R) return T[i].V2;
int m = l + r >> 1;
return min(query2(lc, l, m, L, R), query2(rc, m + 1, r, L, R));
}

int f[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = n; ~i; --i) {
while (!S.empty() && a[i] <= a[S.top()]) ls[S.top()] = i, S.pop();
S.push(i);
}
a[0] = INF; while (!S.empty()) S.pop();
for (int i = n; ~i; --i) {
while (!S.empty() && a[i] >= a[S.top()]) lb[S.top()] = i, S.pop();
S.push(i);
}
update(1, 1, n, 1, 0); f[1] = 0;
for (int i = 2; i <= n; ++i) {
f[i] = f[i - 1];
update1(1, 1, n, 1, i - 2, a[i - 1]);
update2(1, 1, n, 1, i - 2, a[i - 1]);
f[i] = min(f[i], query2(1, 1, n, ls[i], i - 2));
f[i] = min(f[i], query1(1, 1, n, lb[i], i - 2));
update(1, 1, n, i, ++f[i]);
} cout << f[n] << "\n";
return 0;
}