PS

백준 11286번 [절댓값 힙]

binning 2025. 2. 12. 16:07

우선순위 큐

 

규칙에 따른 정렬 ㅡ> 우선순위 큐

struct compare {
	bool operator()(int x, int y) {
		int x_abs = abs(x);
		int y_abs = abs(y);
		if (x_abs == y_abs) return x > y;
		else return x_abs > y_abs;
	}
};

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

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		if (x == 0) {
			if (pq.size() == 0) cout << "0\n";
			else {
				cout << pq.top() << "\n";
				pq.pop();
			}
		}
		else pq.push(x);
	}

	return 0;
}

'PS' 카테고리의 다른 글

백준 2023번 [신기한 소수]  (0) 2025.02.18
백준 1517번 [버블 소트]  (0) 2025.02.12
백준 11003번 [최솟값 찾기]  (0) 2025.02.11
백준 1253번 [좋다]  (0) 2025.02.11
백준 10986번 [나머지 합]  (0) 2025.02.11