알고리즘/BOJ

[c/c++] BOJ 2493번 문제 - 탑

wonjun.Aden 2022. 2. 25. 19:06

https://www.acmicpc.net/problem/2493

 

2493번: 탑

첫째 줄에 탑의 수를 나타내는 정수 N이 주어진다. N은 1 이상 500,000 이하이다. 둘째 줄에는 N개의 탑들의 높이가 직선상에 놓인 순서대로 하나의 빈칸을 사이에 두고 주어진다. 탑들의 높이는 1

www.acmicpc.net

문제

문제풀이

#include <bits/stdc++.h>
using namespace std;
//탑
//N개의 높이가 서로 다른 탑을 수평 직선의 왼쪽부터 오른쪽 방향으로 차례로 세우고, 각 탑의 꼭대기에 송신기 설치.

int main(){
    ios:: sync_with_stdio(false);
    cin.tie(0);

    int n;
    stack<pair<int,int>> st;
    cin >> n;

    for(int i=1;i<=n;i++){
        int num;
        cin >> num;
        while(!st.empty()){
            if(st.top().second > num) {
                cout << st.top().first << ' ';
                break;
            }
            st.pop();
        }
        if(st.empty()){
            cout << "0" << ' ';
        }
        st.push({i,num});

        
    }

    return 0;
}

문제의 포인트

반응형