-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday60.cpp
More file actions
43 lines (38 loc) · 1.07 KB
/
day60.cpp
File metadata and controls
43 lines (38 loc) · 1.07 KB
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
#include <iostream>
#include <queue>
using namespace std;
void findMedians(int N, vector<int>& branches) {
priority_queue<int> maxHeap;
priority_queue<int, vector<int>, greater<int>> minHeap;
for (int i = 0; i < N; i++) {
if (maxHeap.empty() || branches[i] <= maxHeap.top())
maxHeap.push(branches[i]);
else
minHeap.push(branches[i]);
if (maxHeap.size() > minHeap.size() + 1) {
minHeap.push(maxHeap.top());
maxHeap.pop();
} else if (minHeap.size() > maxHeap.size()) {
maxHeap.push(minHeap.top());
minHeap.pop();
}
if (maxHeap.size() == minHeap.size())
cout << (maxHeap.top() + minHeap.top()) / 2 << " ";
else
cout << maxHeap.top() << " ";
}
cout << endl;
}
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector<int> branches(N);
for (int i = 0; i < N; i++)
cin >> branches[i];
findMedians(N, branches);
}
return 0;
}