CF1550 C. Manhattan Subarrays
传送门
思路及分析:
补题代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool judge3(int a,int b,int c)
{
return !( (a<=b&&b<=c) || (a>=b&&b>=c) );
}
bool judge4(int a,int b,int c,int d)
{
return (a>b&&c>a&&d>b&&d<c) || (a<b&&c<a&&d<b&&d>c);
}
int main()
{
int t;
cin>>t;
while(t--)
{
int m;
cin>>m;
int a[m+1];
for(int i=0;i<m;i++)
cin>>a[i];
if(m==1)
cout<<1<<endl;
else if(m==2)
cout<<3<<endl;
else if(m==3)
{
if(judge3(a[0],a[1],a[2]))
cout<<6<<endl;
else cout<<5<<endl;
}
else
{
ll ans=m+m-1+judge3(a[m-3],a[m-2],a[m-1]);//提前处理一和二还有三中的一种情况
for(int i=0;i<m-3;i++)
ans+=judge4(a[i],a[i+1],a[i+2],a[i+3])+judge3(a[i],a[i+1],a[i+2]);
cout<<ans<<endl;
}
}
return 0;
}