- 문제 링크 : boj.kr/17298
- 난이도 : G4
- 태그 : 스택
17298번: 오큰수
첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에 수열 A의 원소 A1, A2, ..., AN (1 ≤ Ai ≤ 1,000,000)이 주어진다.
www.acmicpc.net
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector<int> arr(n);
for(auto &u : arr) cin >> u;
reverse(all(arr));
stack<int> _st;
stack<int> ans;
for(int i = 0; i < n; i++) {
while(!_st.empty()) {
if(_st.top() > arr[i]) {
break;
}
_st.pop();
}
ans.push(_st.empty() ? -1 : _st.top());
_st.push(arr[i]);
}
while(!ans.empty()) {
cout << ans.top() << " ";
ans.pop();
}
return 0;
}
풀이
BOJ2493과 거의 같은 문제이다.
728x90
'PS' 카테고리의 다른 글
BOJ 26524 : 방향 정하기 (0) | 2024.03.30 |
---|---|
BOJ 12796 : 나의 행렬곱셈 답사기 (0) | 2024.03.29 |
BOJ 2493 : 탑 (0) | 2024.03.27 |
BOJ 13422 : 도둑 (0) | 2024.03.26 |
BOJ 7775 : 최종 순위 (1) | 2024.03.25 |