求助 第三个点WA
查看原帖
求助 第三个点WA
782581
Rachel_liu楼主2023/11/17 20:15

测试点3,23行正确答案1,我输出0, 应该是三个连续的峰和谷,为什么输出1 T T

//CF1467B
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MAXN = 3e5 + 5;
ll t, n, a[MAXN];

int main(){
    scanf("%lld", &t);
    while (t--){
        scanf("%lld", &n);
        for (ll i = 1; i <= n; i++){
            scanf("%lld", &a[i]);
        }
        bool flg1 = false, flg2 = false;
        ll ans = 0, cnt = 0;
        for (ll i = 2; i < n; i++){
            if ((a[i-1] > a[i] && a[i+1] > a[i]) || (a[i-1] < a[i] && a[i+1] < a[i])){
                ans++;
                cnt++;
            } else{
                cnt = 0;
            }
            if (cnt >= 2) flg1 = true;
            if (cnt >= 3) flg2 = true;
        }
        if (ans){
            if (flg2){
                ans -= 3;
            } else if (flg1){
                ans -= 2;
            } else{
                ans--;
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}
2023/11/17 20:15
加载中...