AcWing 600. 仰视奶牛
原题链接
简单
作者:
npnb
,
2024-03-29 16:46:54
,
所有人可见
,
阅读 2
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int arr[N], res[N];
typedef pair<int, int> PII;
stack<PII> stk;
int n;
int main(){
cin>>n;
for(int i = 1;i<=n;i++)cin>>arr[i];
for(int i = n;i>=1;i--){
if(stk.empty())res[i] = 0;
else{
if(stk.top().first <= arr[i])
while(stk.size()&&stk.top().first<=arr[i])stk.pop();
if(stk.size())res[i] = stk.top().second;
else res[i] = 0;
}
stk.push({arr[i], i});
}
for(int i = 1;i<=n;i++)cout<<res[i]<<endl;
return 0;
}