본문 바로가기

c++/프로그래머스

백준 2493 : 탑 c++

https://www.acmicpc.net/problem/2493

 

2493번: 탑

첫째 줄에 탑의 수를 나타내는 정수 N이 주어진다. N은 1 이상 500,000 이하이다. 둘째 줄에는 N개의 탑들의 높이가 직선상에 놓인 순서대로 하나의 빈칸을 사이에 두고 주어진다. 탑들의 높이는 1

www.acmicpc.net

구글 참조 해서 풀었다 - stack은 너무 어렵다

 

#include <iostream>
#include <stack>
#include <utility>

using namespace std;


int main() {

	int num;
	scanf("%d", &num);

	int count = 0;
	stack<pair<int, int>> s;
	pair<int, int> p;
	int input;

	while (count < num) 
	{
		scanf("%d", &input);
		p = make_pair(count + 1, input);

		while (!s.empty()) {
			if (s.top().second > input) {
				printf("%d ", s.top().first);
				break;
			}
			s.pop();
		}

		if (s.empty()) 
			printf("0 ");

		s.push(p);
		count++;
	}

	return 0;
}