티스토리 뷰
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을 리턴해야합니다.
}
'Algorithm' 카테고리의 다른 글
Algorithm) SWEA 4038 단어가 등장하는 횟수 c++ (0) | 2023.02.03 |
---|---|
Algorithm) SWEA 2948 문자열 교집합 c++ (1) | 2023.02.03 |
Algorithm) SWEA 2930 힙 c++ (0) | 2023.02.01 |
Algorithm) SWEA 3282 0 / 1 knapsack (0) | 2023.01.31 |
Algorithm) SWEA 3304 최장 공통 부분 수열 (0) | 2023.01.31 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- swea
- 8898
- 코딩테스트
- UIKit
- 알고리즘
- swift
- 도둑질
- 3차원 농부
- 마법사 상어와 파이어스톰
- programmers
- 코테
- 단속카메라
- Algorithm
- Xcode
- 입국심사
- 3000
- 백준 20058
- C++
- programmres
- ios
- 문자열 교집합
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함