单调栈RE求调
代码:
#include<iostream>
#include<stack>
using namespace std;
int h[1000005],ans[1000005];
stack<int> st;
int main() {
int n;
cin>>n;
for(int i=1; i<=n; i++) {
cin>>h[i];
}
for(int i=1; i<=n; i++) {
while(1) {
if((h[i]<=st.top())||(st.empty())) {
st.push(h[i]);
break;
} else {
st.pop();
ans[i]++;
}
}
}
for(int i=1;i<=n;i++){
cout<<ans[i]<<' ';
}
return 0;
}
样例输入:
8
1 2 5 4 3 6 8 7
样例输出:
0 1 1 0 0 3 1 0