728x90
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
// [백준 11279 c++ O] 최대 힙
// 문제: 맥스 힙자료구조를 이용해 입력괴 출력을 하라
// 접근: 맥스힙 뼈대문제 -> 우선순위큐 stl 이용
// 시간복잡도: O(n*logn)
// 풀이:
// 0 입력 -> 빈큐 아니면 최댓값 출력, 없으면 0출력
// 그 외 숫자 맥스힙에 푸쉬
priority_queue<int> maxHeap;
int n;
int main() {
ios::sync_with_stdio(false); // 계산시간 단축 // cin,scanf 같이 쓰면 오류
cin.tie(nullptr); cout.tie(nullptr);// 입출력 시간 단축 // 이것을 쓰면 scanf,printf섞어 쓰면 안됨
cin >> n;
while (n--) {
int num; cin >> num;
if (num == 0) { // 최댓값 출력
if (!maxHeap.empty()) {
cout << maxHeap.top() << '\n';// 맥스힙 최댓값
maxHeap.pop();
}
else { cout << 0 << '\n'; }
} // 맥스힙 푸쉬
else { maxHeap.push(num);}
}
return 0;
}
|
cs |
반응형
'Algorithm_BOJ(백준) > 구현(자료구조)(Data structure)' 카테고리의 다른 글
[백준 4358 c++ V] 생태학 (0) | 2023.01.11 |
---|---|
[백준 1927 c++ V] 최소 힙 (0) | 2021.09.06 |
[백준 20301 c++ O] 반전 요세푸스 (0) | 2021.08.31 |
[백준 18115 c++ V] 카드 놓기 (0) | 2021.08.30 |
[백준 3078 c++ V] 좋은 친구 (0) | 2021.08.30 |