티스토리 뷰

Algorithm

Algorithm) SWEA 2063 중간값 구하기 c++

행복하고 싶은 사람 2023. 2. 2. 11:57

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV-fO0s6ARoDFAXT

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

maxHeap, minHeap을 이용해 중간값을 구하는 문제였습니다.

중간값 구하는 메커니즘은 https://o-tantk.github.io/posts/finding-median/  요 블로그 보시면 쉽게 이해되실겁니다! 

 

tantk land

 

o-tantk.github.io

#include<iostream>
#include <queue>
using namespace std;

int main(int argc, char** argv)
{
    int test_case;
    int T;

    cin>>T;

    for(test_case = 1; test_case <= T; ++test_case)
    {
        long long ans=0;
        int n, first;
        cin>>n>>first;
        priority_queue<int> smaller;
        priority_queue<int, vector<int>,greater<int>> bigger;
        smaller.push(first);
        for(int i=0; i<n; i++) {
            int a,b;
            cin>>a>>b;
            if (a< smaller.top()) {
                smaller.push(a);
            } else {
                bigger.push(a);
            }

            if(smaller.size() < bigger.size()) {
                smaller.push(bigger.top());
                bigger.pop();
            } else if(smaller.size() > bigger.size() + 1) {
                bigger.push(smaller.top());
                smaller.pop();
            }

            if (b< smaller.top()) {
                smaller.push(b);
            } else {
                bigger.push(b);
            }
            if(smaller.size() < bigger.size()) {
                smaller.push(bigger.top());
                bigger.pop();
            } else if(smaller.size() > bigger.size() + 1) {
                bigger.push(smaller.top());
                smaller.pop();
            }
            ans = (ans+ smaller.top())%20171109;
        }
        cout<<"#"<<test_case<<" "<< ans%20171109<<"\n";
    }
    return 0;//정상종료시 반드시 0을 리턴해야합니다.
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
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 29 30 31
글 보관함