Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- chip2chip
- SQL
- hdl
- 실기
- C++
- Bus
- Zynq
- HDLBits
- UNIX
- AMBA BUS
- Backjoon
- 자격증
- java
- Vivado
- FPGA
- amba
- verilog
- 코딩테스트
- 리눅스
- 백준
- Beakjoon
- verilog HDL
- Xilinx
- vitis
- linux
- 정보처리기사
- baekjoon
- axi
- boj
- 정처기
Archives
- Today
- Total
Hueestory
[11286] 절댓값 힙 (C++) 본문
#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번 만큼 출력 후 종료

'PS > BOJ' 카테고리의 다른 글
| [1377] 버블 소트 (C++) (0) | 2024.04.23 |
|---|---|
| [2750] 수 정렬하기 (C++) (0) | 2024.04.23 |
| [2164] 카드2 (C++) (0) | 2024.04.22 |
| [17298] 오큰수 (C++) (0) | 2024.04.22 |
| [1874] 스택 수열 (C++) (0) | 2024.04.22 |
Comments