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

-inn·2022년 1월 8일
0

백준

목록 보기
2/28
post-thumbnail

방법

구현 코드

#include<bits/stdc++.h>
using namespace std;

int n, k;
priority_queue<int> maxh, minh;	// 큰 수부터 출력

// 매번 외친 수 중에서 중간값 출력
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &k);
		if (maxh.size() > minh.size()) {
			minh.push(-k);	// minh 구현하기 위함
		}
		else {
			maxh.push(k);
		}

		if (maxh.size() > 0 && minh.size() > 0) {
			if (maxh.top() > -minh.top()) {
				int tmp_max = maxh.top();
				int tmp_min = -minh.top();
				maxh.pop();
				minh.pop();
				maxh.push(tmp_min);
				minh.push(-tmp_max);	// min에는 음수붙여
			}
		}
		printf("%d\n", maxh.top());
	}
	return 0;
}
  • scanf / printf 해야 시간초과 안남
profile
☁️

0개의 댓글