스택을 이용해 풀이하는 문제, 시간 제한이 1초이므로 1억번 이상의 연산은 불가능하다.
#include <iostream>
#include <stack>
#include <sstream>
using namespace std;
int main() {
int input;
cin >> input;
stack<int> s;
stack<int> result;
int arr[1000000] ;
for(int i = 0 ; i < input ; i++)
{
int tmp;
cin >> tmp;
arr[i] = tmp;
}
for(int i = 0 ; i < input ; i++)
{
while(1)
{
if(s.empty())
{
s.push(i);
break;
}
if(arr[i] > arr[s.top()])
{
arr[s.top()] = arr[i];
s.pop();
}
else
{
s.push(i);
break;
}
}
}
while(!s.empty())
{
int idx = s.top();
arr[idx] = -1;
s.pop();
}
for(int i = 0 ; i < input ; i++)
{
cout << arr[i] <<' ';
}
return 0;
}
'프로그래밍 공부 > 백준 (C++)' 카테고리의 다른 글
[C++/백준 1935번] 후위 표기식 2 (0) | 2024.10.24 |
---|---|
[C++/백준 17298번] 오등큰수 (0) | 2024.10.24 |
[C++/백준 10799번] 쇠막대기 (0) | 2024.10.14 |
[C++ / 백준 17413번] 단어 뒤집기 2 (0) | 2024.10.14 |
[C++/백준 10866번] 덱 (0) | 2024.10.12 |