Little Jay

[C++] 백준 1655번 - 가운데를 말해요 본문

알고리즘/BOJ

[C++] 백준 1655번 - 가운데를 말해요

Jay, Lee 2022. 3. 14. 14:40

while문의 조건을 짜는게 많이 까다로웠던 문제

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

int N;
priority_queue<int, vector<int>, greater<int>> min_heap;
priority_queue<int, vector<int>, less<int>> max_heap;

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	cin >> N;

	while(N--) {
		int score;
		cin >> score;
		
		if (min_heap.empty() || max_heap.empty()) {
			max_heap.push(score);
		}
		else {
			if (score <= max_heap.top())
				max_heap.push(score);
			else if (score >= min_heap.top())
				min_heap.push(score);
			else
				max_heap.push(score);
		}
		while (!(max_heap.size() == min_heap.size() || max_heap.size() == min_heap.size() + 1 )) {
			if (max_heap.size() > min_heap.size()) {
				min_heap.push(max_heap.top());
				max_heap.pop();
			}
			else {
				max_heap.push(min_heap.top());
				min_heap.pop();
			}
		}

		cout << max_heap.top() << endl;
	}

	return 0;
}
Comments