题解#鹏

https://ac.nowcoder.com/acm/contest/19305/1046

传送门-->>鹏

alt

根据题意模拟即可,注意只有上升->平飞->下降,才算一次翻越


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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    
    int ans = 0;
    bool rising = false;  // 标记是否正在上升
    
    for (int i = 1; i < n; i++) {
        if (a[i] > a[i-1]) {
            // 开始上升或继续上升
            rising = true;
        } else if (a[i] < a[i-1] && rising) {
            // 在上升之后遇到下降,完成一次翻越
            ans++;
            rising = false;  // 重置状态
        }
        // 平飞(a[i] == a[i-1])不改变状态
    }
    
    cout << ans;
    return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务