[SWEA] 3000 중간값 구하기

Time Lapse :37min 55sec

3000.cpp

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
32
33
34
35
#include <stdio.h>
#include <queue>
using namespace std;
#define MOD 20171109
int main(int argc, char** argv){
priority_queue<int,vector<int>,greater<int>> bigger;
priority_queue<int,vector<int>,less<int>> smaller;
int mid,N,A,B,answer;
int tc, T;
scanf("%d",&T);
for(tc=1;tc<=T;tc++) {
bigger = priority_queue<int,vector<int>,greater<int>> ();
smaller = priority_queue<int,vector<int>,less<int>> ();
answer = 0;
scanf("%d %d", &N, &mid);
for (int i = 0; i < N; i++) {
scanf("%d %d", &A, &B);
mid > A ? smaller.push(A) : bigger.push(A);
mid > B ? smaller.push(B) : bigger.push(B);
while(smaller.size()>bigger.size()){
bigger.push(mid);
mid = smaller.top();
smaller.pop();
}
while(bigger.size()>smaller.size()){
smaller.push(mid);
mid = bigger.top();
bigger.pop();
}
answer = (answer + mid) % MOD;
}
printf("#%d %d\n",tc, answer);
}
return 0; //정상종료시 반드시 0을 리턴해야 합니다.
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/04/PS/SWEA/3000/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.