반응형

문제출처

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


문제풀이

1
2
3
4
5
6
7
8
9
10
11
/**
 * priority_queue<자료형, 구현체(container), 비교 연산자(compare 함수)>로 정의
 * priority_queue<int, vector<int>, greater<int> > pq;
 * 자료형은 int형이고 값의 저장방식은 vector<int>이며 최소값을 먼저 내놓는다.
 * greater가 최소값을 내뱉고 less가 최대값을 내뱉음
 *
 * 중간값 구하기 알고리즘
 * 최대 힙의 크기 = 최소힙의 크기  or  최소힙의 크기 + 1
 * 최대 힙의 root(최대값) <= 최소힙의 root(최소값)
 * 위 2가지 규칙이 맞지 않는다면 최대힙, 최소힙의 top을 swap한다.
 */
cs


소스코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    priority_queue<intvector<int>, less<int> > max_heap;
    priority_queue<intvector<int>, greater<int> > min_heap;
 
    int n, num;
    cin>>n;
    while(n--) {
        cin>>num;
        if(max_heap.empty() && min_heap.empty()) max_heap.push(num);
        else if(max_heap.size() == min_heap.size()) max_heap.push(num);
        else min_heap.push(num);
 
        if(!max_heap.empty() && !min_heap.empty() && max_heap.top() > min_heap.top()) {
            int tmp=max_heap.top();
            max_heap.pop();
            max_heap.push(min_heap.top());
            min_heap.pop();
            min_heap.push(tmp);
        }
        cout<<max_heap.top()<<"\n";
    }
    return 0;
}
cs


반응형

'Algorithm' 카테고리의 다른 글

[프로그래머스 12936] 줄 서는 방법  (0) 2019.04.23
[프로그래머스 42840] 모의고사(완전탐색)  (0) 2019.04.23
[BOJ 1157] 단어공부  (0) 2019.04.15
[BOJ 2675] 문자열 반복  (0) 2019.04.15
[BOJ 13458] 시험감독  (0) 2019.04.15

+ Recent posts