hades

[Baekjoon] 11286๋ฒˆ: ์ ˆ๋Œ“๊ฐ’ ํž™ ๋ณธ๋ฌธ

๐Ÿ‘Š PS/Algorithm

[Baekjoon] 11286๋ฒˆ: ์ ˆ๋Œ“๊ฐ’ ํž™

hades1 2024. 7. 23. 09:10

๐Ÿฅ… ๋ฌธ์ œ

https://www.acmicpc.net/problem/11286

 

๐Ÿ” ์„ค๊ณ„

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ์กฐ๊ฑด์œผ๋กœ ์ ˆ๋Œ“๊ฐ’์„ ๋น„๊ตํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ์กฐ๊ฑด์œผ๋กœ ์ ˆ๋Œ“๊ฐ’์ด ๊ฐ™๋‹ค๋ฉด ์‹ค์ œ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ์ž‘์€ ๊ฒƒ์ด ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’๋„๋ก ํ•œ๋‹ค.

 

๐Ÿ‘Š ํ’€์ด

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int n, x;

struct compare {
	bool operator()(int a, int b) {
		if (abs(a) > abs(b)) {
			return true;
		}
		else if (abs(a) == abs(b)) {
			return a > b;
		}
		else {
			return false;
		}
	}
};

priority_queue<int, vector<int>, compare> pq;


int main(void)
{	
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> x;
		if (x == 0) {
			if (pq.empty()) {
				cout << 0 << "\n";
			}
			else {
				cout << pq.top() << "\n";
				pq.pop();
			}
		}
		else {
			pq.push(x);
		}
	}
	return 0;
}

 

๐Ÿ“ ๋ฉ”๋ชจ

์šฐ์„ ์ˆœ์œ„ ํ์˜ compare๋Š” ๊ตฌ์กฐ์ฒด๋กœ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋ฐฐ์—ด์—์„œ ์ •๋ ฌํ•˜๋Š” ํ•จ์ˆ˜์™€ ๋ฐ˜๋Œ€๋กœ ์ƒ๊ฐํ•ด์•ผ ํ•œ๋‹ค.