-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1467b.cpp
More file actions
64 lines (61 loc) · 2.01 KB
/
1467b.cpp
File metadata and controls
64 lines (61 loc) · 2.01 KB
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
/**
* author: vishnus
* created: 2022-08-17
**/
#include <bits/stdc++.h>
using namespace std;
// Idea: when you change a value, its value and its neighbours are the ones that are possibly
// affected. What is the optimal value for a[i]? It is either a[i + 1] or a[i - 1]. Why?
// Because it is guaranteed to remove two hills/valleys, a[i] and whichever element we are
// setting it to. Other one may be removed.
int main() {
#ifdef ONLINE_JUDGE
ios::sync_with_stdio(false);
cin.tie(0);
#endif
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
vector<int> a(n);
for (auto &e : a) {
cin >> e;
}
vector<int> h(n), v(n), any(n);
int tot = 0;
for (int i = 1; i < n - 1; i++) {
h[i] = (a[i] > a[i - 1] && a[i] > a[i + 1]);
v[i] = (a[i] < a[i - 1] && a[i] < a[i + 1]);
tot += h[i] + v[i];
any[i] = h[i] | v[i];
}
int red = 0;
for (int i = 1; i < n - 1; i++) {
// Change a[i] to either a[i - 1] or a[i + 1] and check.
int old_val = a[i];
a[i] = a[i - 1];
int cur_tot = 0;
if (i != 1) {
cur_tot += (a[i - 1] > a[i - 2] && a[i - 1] > a[i]) || (a[i - 1] < a[i - 2] && a[i - 1] < a[i]);
}
if (i != n - 2) {
cur_tot += (a[i + 1] > a[i + 2] && a[i + 1] > a[i]) || (a[i + 1] < a[i + 2] && a[i + 1] < a[i]);
}
cur_tot += (a[i] > a[i - 1] && a[i] > a[i + 1]) || (a[i] < a[i - 1] && a[i] < a[i + 1]);
red = max(red, any[i] + any[i + 1] + any[i - 1] - cur_tot);
a[i] = a[i + 1];
cur_tot = 0;
if (i != 1) {
cur_tot += (a[i - 1] > a[i - 2] && a[i - 1] > a[i]) || (a[i - 1] < a[i - 2] && a[i - 1] < a[i]);
}
if (i != n - 2) {
cur_tot += (a[i + 1] > a[i + 2] && a[i + 1] > a[i]) || (a[i + 1] < a[i + 2] && a[i + 1] < a[i]);
}
cur_tot += (a[i] > a[i - 1] && a[i] > a[i + 1]) || (a[i] < a[i - 1] && a[i] < a[i + 1]);
red = max(red, any[i] + any[i + 1] + any[i - 1] - cur_tot);
a[i] = old_val;
}
cout << tot - red << '\n';
}
}