http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4107
题意:至多删除一个点,使得破音数量最少
题解:前缀+后缀+枚举
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a[N],pre[N],suf[N];
char str;
struct node{};
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(pre,0,sizeof(pre));
memset(suf,0,sizeof(suf));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
if(n<=2){
cout<<0<<endl;
continue;
}
for(int i=2;i<n;i++){
pre[i]=pre[i-1]+(a[i-1]<a[i]&&a[i+1]<a[i]);
}
for(int i=n-1;i>1;i--){
suf[i]=suf[i+1]+(a[i-1]<a[i]&&a[i+1]<a[i]);
}
ans=min(suf[2]-(a[1]<a[2]&&a[3]<a[2]),pre[n-1]-(a[n-2]<a[n-1]&&a[n]<a[n-1]));
ans=min(ans,min(pre[n-3]+(a[n-3]<a[n-2]&&a[n]<a[n-2]),suf[4]+(a[1]<a[3]&&a[4]<a[3])));
for(int i=3;i<=n-2;i++){
//cout<<pre[i]<<" "<<suf[i]<<" "<<pre[i-2]+suf[i+2]+(a[i-2]<a[i-1]&&a[i+1]<a[i-1])+(a[i-1]<a[i+1]&&a[i+2]<a[i+1])<<endl;
ans=min(ans,pre[i-2]+suf[i+2]+(a[i-2]<a[i-1]&&a[i+1]<a[i-1])+(a[i-1]<a[i+1]&&a[i+2]<a[i+1]));
}
cout<<min(ans,pre[n-1])<<endl;
}
#ifdef DEBUG
printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
//cout << "Hello world!" << endl;
return 0;
}