链接
题意:
定义山峰(两边低中间高)和山谷(两边高中间低)。问最少这两种的和是多少?需要可以操作一个数。
分析:
首先我们需要先计算出不改变数会有多少种,和为多少?这个东西是好计算出来的,(就从头跑一遍)
然后我们看每个数改变会改变多少,当下标为x的数改变时他只影响中间点为(x)和(x-1),(x+1).那我们我们就需要看改变这个数会变成什么,为了减少数量有两种选择,一种是变成前一个数,一种是变成后面的那个数,我们只需要判断出来即可。然后枚举一下改变的数。
但是做的时候也不知道在想神马?
// Problem: B. Hills And Valleys
// Contest: Codeforces - Codeforces Round #695 (Div. 2)
// URL: https://codeforces.com/contest/1467/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
/// 欲戴皇冠,必承其重。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
#define x first
#define y second
#define sf scanf
#define pf printf
#define PI acos(-1)
#define inf 0x3f3f3f3f
#define lowbit(x) ((-x)&x)
#define mem(a,x) memset(a,x,sizeof(a))
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<=(b);++i)
#define repr(i,b,a) for(int i=int(b);i>=(a);--i)
#define debug(x) cout << #x << ": " << x << endl;
const int MOD = 998244353;
const int mod = 998244353;
const int N = 3e5 + 10;
const int dx[] = {0, 1, -1, 0, 0};
const int dy[] = {0, 0, 0, 1, -1};
const int dz[] = {1, -1, 0, 0, 0, 0 };
int day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
ll n, m;
string str;
ll a[N];
ll ans,tmp;
bool check1(ll x){
if(x>1&&a[x]>a[x-1]&&x<n&&a[x]>a[x+1]) return 1;
return 0;
}
bool check2(ll x){
if(x>1&&a[x]<a[x-1]&&x<n&&a[x]<a[x+1]) return 1;
return 0;
}
ll juedg(ll x,ll y){
ll tx=a[x];
ll l,r;
l=r=0;
l+=check1(x)||check2(x);
l+=check1(x-1)||check2(x-1);
l+=check1(x+1)||check2(x+1);
a[x]=a[x+y];
r+=check1(x)||check2(x);
r+=check1(x-1)||check2(x-1);
r+=check1(x+1)||check2(x+1);
a[x]=tx;
return l-r;
}
void solve()
{
cin>>n;
ans=0,tmp=0;
for(int i = 1;i<=n;i++) scanf("%lld",&a[i]);
for(int i=2;i<n;i++) ans+=check1(i)||check2(i),tmp=max(tmp,max(juedg(i,-1),juedg(i,1)));
cout<<ans-tmp<<endl;
}
int main()
{
ll t = 1;
scanf("%lld", &t);
while(t--)
{
solve();
}
return 0;
}