Running Median
poj 3784
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1652 | Accepted: 818 |
Description
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (middle value) of the elements received so far.
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by an odd decimal integer M, (1 ≤ M ≤ 9999), giving the total number of signed integers to be processed. The remaining line(s) in the dataset consists of the values, 10 per line, separated by a single space. The last line in the dataset may contain less than 10 values.
Output
For each data set the first line of output contains the data set number, a single space and the number of medians output (which should be one-half the number of input values plus one). The output medians will be on the following lines, 10 per line separated by a single space. The last line may have less than 10 elements, but at least 1 element. There should be no blank lines in the output.
Sample Input
3 1 9 1 2 3 4 5 6 7 8 9 2 9 9 8 7 6 5 4 3 2 1 3 23 23 41 13 22 -3 24 -31 -11 -8 -7 3 5 103 211 -311 -45 -67 -73 -81 -99 -33 24 56
Sample Output
1 5 1 2 3 4 5 2 5 9 8 7 6 5 3 12 23 23 22 22 13 3 5 5 3 -3 -7 -3
Source
[Submit] [Go Back] [Status] [Discuss]
————————————————————我是分割线——————————————————————————
维护两个堆;
一个大根堆,一个小根堆。
1 /* 2 problem:poj 3784 3 by:S.B.S. 4 */ 5 #include<iostream> 6 #include<cstdio> 7 #include<cstring> 8 #include<cmath> 9 #include<algorithm> 10 #include<queue> 11 #include<cstdlib> 12 #include<iomanip> 13 #include<cassert> 14 #include<climits> 15 #define maxn 10001 16 #define F(i,j,k) for(int i=j;i<=k;i++) 17 #define M(a,b) memset(a,b,sizeof(a)) 18 #define FF(i,j,k) for(int i=j;i>=k;i--) 19 #define inf 0x7fffffff 20 #define p 23333333333333333 21 using namespace std; 22 int read(){ 23 int x=0,f=1;char ch=getchar(); 24 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 25 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 26 return x*f; 27 } 28 priority_queue<int,vector<int>,greater<int> > q1; 29 priority_queue<int,vector<int>,less<int> > q2; 30 vector<int> g; 31 void add(int x) 32 { 33 if(q1.empty()){ 34 q1.push(x); 35 return; 36 } 37 if(x>q1.top()) q1.push(x); 38 else q2.push(x); 39 while(q1.size()<q2.size()){ 40 q1.push(q2.top()); 41 q2.pop(); 42 } 43 while(q1.size()>q2.size()+1) { 44 q2.push(q1.top()); 45 q1.pop(); 46 } 47 } 48 int main() 49 { 50 std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y; 51 // freopen("data.in","r",stdin); 52 // freopen("data.out","w",stdout); 53 int t,c,n,x; 54 cin>>t; 55 while(t--){ 56 while(!q1.empty()) q1.pop(); 57 while(!q2.empty()) q2.pop(); 58 g.clear(); 59 cin>>c>>n; 60 F(i,0,n-1){ 61 cin>>x; 62 add(x); 63 if(i%2==0) g.push_back(q1.top()); 64 } 65 cout<<c<<" "<< (n+1)/2 <<endl; 66 F(i,0,g.size()-1){ 67 if(i>0&& i%10==0) cout<<endl; 68 if(i%10) cout<<" "; 69 cout<<g[i]; 70 } 71 cout<<endl; 72 } 73 return 0; 74 }