题解 | #大雨吃小鱼#

大雨吃小鱼

https://www.nowcoder.com/practice/77199defc4b74b24b8ebf6244e1793de

#include <iostream>
#include <utility>
#include <vector>
#include <stack>
using namespace std;

int main() {
    int m;
    cin>>m;
    vector<int> fash(m, 0);
    for(int i=0; i<m; i++)
    {
        cin>>fash[i];
    }

    stack<pair<int, int>> s;
    pair<int, int> tops;
    int res=0;
    
    for(int i=m-1;i>=0; i--)
    {
        int counts = 0;
        while(!s.empty() && fash[i] > s.top().first)
        {
            tops = s.top();
            s.pop();
            counts = max(counts+1, tops.second);
        }
        s.push({fash[i], counts});
        res = max(res, counts);
    }
    cout<<res<<endl;
    return 0;
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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