코딩 테스트
백준 2493 탑
__sapar
2024. 9. 9. 00:12
스택 문제 어렵네... 생각이 안남
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
using namespace std;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int num;
cin >> num;
stack<pair<int, int>> sta;
for (int i = 0; i < num; ++i)
{
int n;
cin >> n;
while (!sta.empty())
{
if (sta.top().first > n)
{
cout << sta.top().second + 1 << " ";
break;
}
sta.pop();
}
if (sta.empty())
{
cout << "0 ";
}
sta.push(make_pair(n,i));
}
return 0;
}