Hueestory

[11286] 절댓값 힙 (C++) 본문

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번 만큼 출력 후 종료

 

'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