EOJ 3213 Stack

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

typedef pair<int,int> pii;
const int maxN=1e6+5;
stack<pii> s;
int res[maxN];

int main(){
    int n,u;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&u);
        while(!s.empty()&&s.top().first<u){
            res[s.top().second]=i+1;
            s.pop();
        }
        s.push(pii(u,i));
    }
    while(!s.empty()){
        res[s.top().second]=0;
        s.pop();
    }
    for(int i=0;i<n;i++)printf("%d\n",res[i]);
}

猜你喜欢

转载自www.cnblogs.com/TAMING/p/9363395.html