forked from codingCapricorn/Hactoberfest-2020-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMedian of Running Stream
87 lines (75 loc) · 2.58 KB
/
Median of Running Stream
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
QUESTION:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
You are given a running data stream of n integers. You read all integers from that running data stream and find effective median of elements read so far in efficient way. All numbers are unique in the datastream. Print only the integer part of the median.
Input Format
First line contains integer t as number of test cases. Each test case contains following input. First line contains integer n which represents the length of the data stream and next line contains n space separated integers.
Constraints
1 < t < 100
1< n < 10000
Output Format
Print the effective median of running data stream..
Sample Input
1
6
5 15 1 3 2 8
Sample Output
5 10 5 4 3 4
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
/* code here */
int t;
cin>>t;
while(t-->0){
int n;
cin>>n;
priority_queue<int>maxheap;
priority_queue<int ,vector<int>,greater<int>>minheap;
int d;
cin>>d;
maxheap.push(d);
int median = d;
cout<<median<<" ";
while(n-->1){
cin>>d;
if(maxheap.size()>minheap.size()){
if(d<median){
minheap.push(maxheap.top());
maxheap.pop();
maxheap.push(d);
}
else{
minheap.push(d);
}
median=(maxheap.top()+minheap.top())/2;
}
else if(maxheap.size()==minheap.size()){
if(d<median){
maxheap.push(d);
median=maxheap.top();
}
else{
minheap.push(d);
median=minheap.top();
}
}
else{
if(d>median){
maxheap.push(minheap.top());
minheap.pop();
minheap.push(d);
}
else{
maxheap.push(d);
}
median=(maxheap.top()+minheap.top())/2;
}
cout<<median<<" ";
}
cout<<endl;
}
return 0;
}