题解 | #愤怒的小鸟#

愤怒的小鸟

https://www.nowcoder.com/practice/597c32f0b3cf43beb1d01e7ddd87cc32

单调栈

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

int main() {
    int n;
    cin>> n;
    vector<int> h(n);
    for(int i=0;i<n;i++){
        cin>> h[i];
    }
    stack<int> stk; // 单调减,保存当前能炸到i的点的个数
    vector<int> res(n);
    for(int i=0;i<n;i++){
        while(!stk.empty()&& h[i]>stk.top()){
            stk.pop();
        }
        res[i] += i - stk.size();
        stk.push(h[i]);
    }
    stk = stack<int>();
    for(int i=n-1;i>=0;i--){
        while(!stk.empty()&&h[i]>stk.top()){
            stk.pop();
        }
        res[i] += (n-1 -i) - stk.size();
        stk.push(h[i]);
    }
    for(int i=0;i<n;i++){
        cout << res[i] << " ";
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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