-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathb.cpp
49 lines (45 loc) · 1.12 KB
/
b.cpp
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
#include <bits/stdc++.h>
using namespace std;
int T, N, a[300000], stat[300000];
int test(int i){
if(i == 0 || i == N-1) return 0;
if(a[i] < a[i-1] && a[i] < a[i+1]){
return 1;
}
if(a[i] > a[i-1] && a[i] > a[i+1]){
return 1;
}
return 0;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while(T--){
cin >> N;
for(int i = 0; i < N; i++){
cin >> a[i];
}
int hv = 0;
for(int i = 1; i < N-1; i++){
if(test(i) > 0){
hv++;
}
}
int ans = hv;
for(int i = 1; i < N-1; i++){
if(test(i) > 0){
int csum = test(i-1) + test(i) + test(i+1);
int og = a[i];
a[i] = a[i-1];
int diff = test(i-1) + test(i) + test(i+1) - csum;
ans = min(ans, hv+diff);
a[i] = a[i+1];
diff = test(i-1) + test(i) + test(i+1) - csum;
ans = min(ans, hv+diff);
a[i] = og;
}
}
cout << ans << endl;
}
}