728x90
문제 11286번
https://www.acmicpc.net/problem/11286
구조체와 operator 함수를 이용해서 정렬하는 방법을 정의해준다.
절댓값이 같다면 가장 작은 원소를 리턴해주고, 아니면 절댓값이 작은 원소를 리턴해준다.
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct compare {
bool operator()(int a, int b) {
if (abs(a) == abs(b)) {
return a > b;
}
return abs(a) > abs(b);
}
};
int main() {
int n, x;
vector<int> v;
priority_queue<int, vector<int>, compare> pq;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
if (x == 0) {
if (pq.empty()) {
v.push_back(0);
}
else {
v.push_back(pq.top());
pq.pop();
}
}
else {
pq.push(x);
}
}
for (int i = 0; i < v.size(); i++) {
cout << v[i] << '\n';
}
return 0;
}
728x90
'백준 > C++' 카테고리의 다른 글
[C++] 백준 15649번 - N과 M(1) (0) | 2024.08.26 |
---|---|
[C++] 백준 1269번 - 대칭 차집합 (0) | 2024.08.22 |
[C++] 백준 1927번 - 최소 힙 (0) | 2024.08.18 |
[C++] 백준 11279번 - 최대 힙 (0) | 2024.08.16 |
[C++] 백준 2145번 - 숫자 놀이 (0) | 2024.08.15 |