题目
https://codeforces.com/problemset/problem/1467/B
就是一列高高低低的数字,改变其中的一个数字,使得这一列中山峰+山谷的数目最小
思路
其实就是先找到原来的总数目,然后从1—n-1位一个数字一个数字的改变,①变成左边的数字,求出这时数目的最小的,②变成右边的数字,求出这时候数目最小的,不断往后遍历求出,得到最后的结果
感觉虽然能大致想出来,但是具体的代码实现还是要加强
代码
//平平无奇复制粘贴。。
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e5;
int a[N], n;
int isValley(int i) {
return (i > 0 && i < n - 1 && a[i] < a[i - 1] && a[i] < a[i + 1]);
}
int isHill(int i) {
return (i > 0 && i < n - 1 && a[i] > a[i - 1] && a[i] > a[i + 1]);
}
int solveTestCase() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
int is[n] = {};
int s = 0;
for (int i = 1; i < n - 1; i++) {
if (isHill(i) || isValley(i))
is[i] = 1, s++;
}
int ans = s;
for (int i = 1; i < n - 1; i++) {
int temp = a[i];
a[i] = a[i - 1];
ans = min(ans, s - is[i - 1] - is[i] - is[i + 1] + isHill(i - 1) + isValley(i - 1) + isHill(i) + isValley(i) + isHill(i + 1) + isValley(i + 1));
a[i] = a[i + 1];
ans = min(ans, s - is[i - 1] - is[i] - is[i + 1] + isHill(i - 1) + isValley(i - 1) + isHill(i) + isValley(i) + isHill(i + 1) + isValley(i + 1));
a[i] = temp;
}
cout << ans << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
cin >> t;
while (t--)
solveTestCase();
}