01-25 10:51
Recent Posts
Recent Comments
관리 메뉴

너와나의 관심사

백준 1655 우선순위큐를 활용한 중간값 본문

카테고리 없음

백준 1655 우선순위큐를 활용한 중간값

벤치마킹 2021. 3. 3. 10:02
#include <stdio.h>
#include <iostream> 
#include <vector>
#include <queue>
#include <functional>
#include <algorithm>

using namespace std;

int main(void){

	
	
    
    freopen("input.txt", "r", stdin);
	
	int N, val;
	cin >> N;
    
    //내림 차순  // top- > 5 4 3 2 1  
    priority_queue< int, vector<int>, less<int> > up_pq;

    //오름 차순  // top -> 1 2 3 4 5 
    priority_queue< int, vector<int>, greater<int> > down_pq;
   
    for (int i = 0 ; i < N ; i ++){
        scanf("%d", &val);

        if ( down_pq.size() == up_pq.size() ){
            up_pq.push(val);
        }else{
            down_pq.push(val);
        }

        if (!up_pq.empty() && !down_pq.empty() && (down_pq.top() < up_pq.top())){
            
            register int up_val = up_pq.top();
            register int down_val = down_pq.top();

            up_pq.pop();
            down_pq.pop();

            down_pq.push(up_val);
            up_pq.push(down_val);

        }

        cout<< up_pq.top()<<"\n";

    }


	return 0;
}

 

Comments