본문 바로가기
Study/Algorithm

[Algorithm-stack] 순서정렬

by Hoony-Daddy 2023. 8. 31.
728x90
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#include <cstdio>
#include <stack>
#include <ranges>

int main()
{
    int n;
    std::cin>>n;
    std::vector<int> heights(n+1);
    std::stack<int> s;
    std::vector<int> res(n+1,0);
    for(int i=1;i<=n;i++){
        std::cin >> heights[i];
    }
    for(int i=n;i>0;i--){
        while(!s.empty() && heights[s.top()]< heights[i]){
                res[s.top()] = i;
                s.pop();
                
        }
        s.push(i);
    }
   for(int i=1;i<=n;i++){
        std::cout<<res[i]<<" ";
    }

    
    return 0;
}