forked from tony9402/baekjoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
52 lines (46 loc) · 1.33 KB
/
main.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Authored by : tony9402
// Co-authored by : -
// Link : http://boj.kr/c5acf6a6a2f24b4ca0c415fbe1947571
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
while(t--) {
int n; cin >> n;
priority_queue<int> L;
priority_queue<int, vector<int>, greater<int>> R;
vector<int> answer;
for(int i=0;i<n;i++) {
int x; cin >> x;
R.push(x);
while(L.size() < R.size()) {
L.push(R.top());
R.pop();
}
while(L.size() > R.size() + 1) {
R.push(L.top());
L.pop();
}
while(!L.empty() || !R.empty()) {
int valueL = L.top();
int valueR = R.top();
if(valueL <= valueR) break;
L.pop();
R.pop();
L.push(valueR);
R.push(valueL);
}
if(i % 2 == 0) answer.push_back(L.top());
}
cout << answer.size() << '\n';
for(int i=0;i<(int)answer.size();i++) {
cout << answer[i];
if(i + 1 != (int)answer.size() && (i + 1) % 10 == 0) cout << '\n';
else cout << ' ';
}
cout << '\n';
}
return 0;
}