Skip to content

Commit

Permalink
Merge pull request #43 from AlgoLeadMe/14-YIM2UL2ET
Browse files Browse the repository at this point in the history
14-YIM2UL2ET
  • Loading branch information
tgyuuAn authored Jun 27, 2024
2 parents ef40fc8 + d536d12 commit eee8b46
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions YIM2UL2ET/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
| 11μ°¨μ‹œ | 2024.03.18 | μŠ€νƒ | [BOJ 1918](https://www.acmicpc.net/problem/1918) | [BOJ 1918 풀이](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/36) |
| 12μ°¨μ‹œ | 2024.03.28 | λΉ„νŠΈλ§ˆμŠ€ν‚Ή | [BOJ 11723](https://www.acmicpc.net/problem/11723) | [BOJ 11723 풀이](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/39) |
| 13μ°¨μ‹œ | 2024.04.01 | BFS | [BOJ 7569](https://www.acmicpc.net/problem/7569) | [BOJ 7569 풀이](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/42) |
| 14μ°¨μ‹œ | 2024.04.04 | μš°μ„ μˆœμœ„ 큐 | [BOJ 11286](https://www.acmicpc.net/problem/11286) | [BOJ 11286 풀이](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/43) |
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <vector>

void cre_heap(std::vector <int> &heap, int key) {
int k = heap.size();
heap.push_back(key);
while (k > 1) {
if (abs(heap[k/2]) > abs(heap[k]) || (abs(heap[k/2]) == abs(heap[k]) && heap[k/2] > heap[k])) {
std::swap(heap[k], heap[k/2]);
k /= 2;
}
else break;
}
return;
}

void del_heap(std::vector <int> &heap) {
int k = 1;
heap[1] = heap.back(), heap.pop_back();
while (k * 2 <= heap.size()) {
if (k * 2 + 1 < heap.size() && (abs(heap[k*2+1]) < abs(heap[k*2]) || (abs(heap[k*2+1]) == abs(heap[k*2]) && heap[k*2+1] < heap[k*2])))
k = k*2+1;
else k = k*2;

if (abs(heap[k/2]) > abs(heap[k]) || (abs(heap[k/2]) == abs(heap[k]) && heap[k/2] > heap[k]))
std::swap(heap[k/2], heap[k]);
else break;
}
return;
}

int main(void) {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);

int n, k, x;
std::vector <int> abs_heap = {0};

std::cin >> n;
for (int i = 0; i < n; i++) {
std::cin >> x;
if (abs(x) > 0) cre_heap(abs_heap, x);
else if (abs_heap.size() > 1) {
std::cout << abs_heap[1] << '\n';
del_heap(abs_heap);
}
else std::cout << 0 << '\n';
}

}

0 comments on commit eee8b46

Please sign in to comment.