CF EDU 111 C - Manhattan Subarrays
C - Manhattan Subarrays
枚举
多观察数据,发现当子数组中元素个数 >= 5 时一定不满足条件,因此枚举子数组个数为 3, 4 即可
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
const int INF = 2e9;
int n;
int a[N];
bool check(int l, int r)
{
for (int i = l; i <= r; i++)
for (int j = i + 1; j <= r; j++)
for (int k = j + 1; k <= r; k++)
{
if (a[i] <= a[j] && a[j] <= a[k])
return false;
if (a[i] >= a[j] && a[j] >= a[k])
return false;
}
return true;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T--)
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
int ans = 2 * n - 1;
for (int l = 1; l + 2 <= n; l++)
if (check(l, l + 2))
ans++;
for (int l = 1; l + 3 <= n; l++)
if (check(l, l + 3))
ans++;
cout << ans << endl;
}
return 0;
}