백준 algorithm

백준 11279 최대힙 C++

cosmohoo 2022. 4. 2. 23:58
반응형

 

문제 설명

 

 

예제 입력 1 복사

13
0
1
2
0
0
3
2
1
0
0
0
0
0

예제 출력 1 복사

0
2
1
3
2
1
0
0

 

 

=> 최대힙을 구현할 수 있는지에 대한 문제였습니ㅏㄷ. 

=> C++에는 queue 헤더 안에 priority_queue 자료형을 사용 할 수 있습니다. 

=> 해당 자료형을 이용해서 주어진 조건에 따라 출력문을 출력하면 되는 문제였습니다. 

=> 시간초과가 걸릴 경우, 

cin.tie(0);
ios_base::sync_with_stdio(false);

해당 구문을 입력해줄 경우 시간초과에 걸리지 않게 됩니다.

 

#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iostream>
#include <queue>
#include <utility>

using namespace std;

int M;



int main()
{ 
	priority_queue<int> pq;
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	cin >> M;
	for (int i = 0; i < M; i++)
	{
		int tmp;
		cin >> tmp;

		if (tmp == 0)//max값 제거 
		{
			if (pq.empty()) cout << '0' << '\n';
			else
			{
				cout << pq.top() << '\n';
				pq.pop();
			}
		}
		else//삽입
		{
			pq.push(tmp);
		}
	}
	return 0;
}
반응형