[문제]
https://www.acmicpc.net/problem/2493
[풀이]
본인의 바로 왼쪽에 있는 탑부터 비교하도록 하기 위해 stack을 이용한다. 현재 탑의 높이보다 크면 출력, 작으면 pop()을 해주고, stack이 비면 왼쪽에 있는 탑들 중 신호를 받을 탑이 없다는 것이므로 0을 출력한다.
[코드]
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace::std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
stack<pair<int, int>> top;
cin >> n;
int height;
for(int i=1; i<=n; i++){
cin >> height;
while(!top.empty()){
if(top.top().second > height){
cout << top.top().first << " ";
break;
}
top.pop();
}
if(top.empty())
cout << "0 ";
top.push({i, height});
}
}
'알고리즘 공부 및 문제 풀이 > 백준(BOJ)' 카테고리의 다른 글
[boj] 백준 16236 아기 상어 - BFS (0) | 2022.04.01 |
---|---|
[boj] 백준 2252 줄 세우기 - 위상정렬 (0) | 2022.03.30 |
[boj] 백준 2565 전깃줄 - dp, 증가하는 수열 (0) | 2022.03.18 |
[boj] 백준 16234 인구 이동 - BFS (0) | 2022.03.17 |
[boj] 백준 2110 공유기 설치 - 이분탐색 (0) | 2022.03.17 |