题解 | #愤怒的小鸟#
愤怒的小鸟
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")
传音控股公司福利 317人发布
