PS(중단)/BOJ
[11286] 절댓값 힙 (C++)
히명
2024. 4. 22. 16:19
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
struct compare {
bool operator()(int n1, int n2) {
int abs1 = abs(n1);
int abs2 = abs(n2);
if (abs1 == abs2) {
return n1 > n2;
}
else {
return abs1 > abs2;
}
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n; cin >> n;
priority_queue<int, vector<int>, compare> q;
for (int i = 0; i < n; i++) {
int input; cin >> input;
if (input == 0) {
if (q.empty()) {
cout << "0\n";
}
else {
cout << q.top() << "\n";
q.pop();
}
}
else {
q.push(input);
}
}
}
1. priority queue에 0을 정수를 입력받음
2. 0이 입력되면 queue에 있는 정수 중 절댓값이 가장 작은 수를 출력, 만약 음수와 양수가 있다면 음수부터 출력
3. 처음 입력받은 n번 만큼 출력 후 종료