https://www.acmicpc.net/problem/2493
시작 지점을 stack에 같이 저장하는 것이 관건이었던 문제였다.
스택의 첫번째값에 큰 수를 넣어 0부터 check 할 수 있도록 하였다.
#include<iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <climits>
#include <queue>
#include <set>
#include <cmath>
#include <stack>
#include <list>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
// 입력값 받을 배열
int arr[500002];
// 정답 배열
int answer[500002] = {0};
// 스택
stack<pair<int, int> > stk;
cin >> n;
stk.push(make_pair(987654321, 0));
for (int i = 1; i <= n; i++) {
cin >> arr[i];
while (stk.top().first < arr[i]) {
stk.pop();
}
answer[i] = stk.top().second;
stk.push(make_pair(arr[i], i));
cout << answer[i] << " ";
}
return 0;
}
'PS > BOJ' 카테고리의 다른 글
[백준] 17298. 오큰수(c++) (0) | 2025.02.03 |
---|---|
[백준] 6198. 옥상 정원 꾸미기(c++) (0) | 2025.02.03 |
[백준] 3015. 오아시스 재결합(c++) (0) | 2025.02.02 |
[백준] 6198. 옥상정원 꾸미기(c++) (0) | 2025.01.31 |
[백준] 1874. 스택 수열(c++) (0) | 2025.01.26 |