Time Limit: 6000MS | Memory Limit: 10000K | |
Total Submissions: 7095 | Accepted: 2184 | |
Case Time Limit: 4000MS |
Description
In a speech contest, when a contestant finishes his speech, the judges will then grade his performance. The staff remove the highest grade and the lowest grade and compute the average of the rest as the contestant’s final grade. This is an easy problem because usually there are only several judges.
Let’s consider a generalized form of the problem above. Given n positive integers, remove the greatest n1 ones and the least n2 ones, and compute the average of the rest.
Input
The input consists of several test cases. Each test case consists two lines. The first line contains three integers n1, n2 and n (1 ≤ n1, n2 ≤ 10, n1 + n2 < n ≤ 5,000,000) separate by a single space. The second line contains n positive integers ai (1 ≤ ai ≤ 108 for all i s.t. 1 ≤ i ≤ n) separated by a single space. The last test case is followed by three zeroes.
Output
For each test case, output the average rounded to six digits after decimal point in a separate line.
Sample Input
1 2 5 1 2 3 4 5 4 2 10 2121187 902 485 531 843 582 652 926 220 155 0 0 0
Sample Output
3.500000 562.500000
Hint
This problem has very large input data. scanf and printf are recommended for C++ I/O.
The memory limit might not allow you to store everything in the memory.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf=(1<<30);
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int n1,n2,n;
while(scanf("%d%d%d",&n1,&n2,&n)==3&&n)
{
int _max[15],_min[15];
for(int i=0;i<15;i++) _max[i]=inf,_min[i]=-inf;
make_heap(_max,_max+15);//最大堆,求前n2小的数
make_heap(_min,_min+15,cmp);//最小堆,求前n1大的数
double cnt=0;
for(int i=0;i<n;i++)
{
int x;scanf("%d",&x);
cnt+=x;
if(x<_max[0])
{
pop_heap(_max,_max+15);
_max[14]=x;
push_heap(_max,_max+15);
}
if(x>_min[0])
{
pop_heap(_min,_min+15,cmp);
_min[14]=x;
push_heap(_min,_min+15,cmp);
}
}
int x[15];
for(int i=0;i<15;i++)
{
x[i]=_max[0];
pop_heap(_max,_max+15-i);
}
for(int i=14,j=0;i>=0&&j<n2;i--,j++) cnt-=x[i];
for(int i=0;i<15;i++)
{
x[i]=_min[0];
pop_heap(_min,_min+15-i,cmp);
}
for(int i=14,j=0;i>=0&&j<n1;i--,j++) cnt-=x[i];
printf("%lf/n",cnt/(n-n1-n2));
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int inf=(1<<30);
int heap_max[15],heap_min[15];
int hs_max,hs_min;
//最小堆,从1开始
void sink_min(int p)//下沉操作
{
int q=p<<1;//q是p的左儿子节点
int a=heap_min[p];//要下沉的数
while(q<=hs_min)
{
if(q<hs_min&&heap_min[q+1]<heap_min[q]) q++;
if(heap_min[q]>=a) break;
heap_min[p]=heap_min[q];
p=q;
q=p<<1;
}
heap_min[p]=a;
}
int del_min()//去最小值,并且删除最小值
{
int r=heap_min[1];
heap_min[1]=heap_min[hs_min--];
sink_min(1);
return r;
}
void swim_min(int p)//上浮操作
{
int q=p>>1;//q是p的父亲节点
int a=heap_min[p];//要上浮的数
while(q&&a<heap_min[q])//如果比父亲节点小
{
heap_min[p]=heap_min[q];//上浮
p=q;q=p>>1;
}
heap_min[p]=a;
}
void insert_min(int a)//插入元素
{
heap_min[++hs_min]=a;
swim_min(hs_min);
}
void init_min()
{
hs_min=0;
}
//最大堆,从1开始
void sink_max(int p)//下沉操作
{
int q=p<<1;//q是p的左儿子节点
int a=heap_max[p];//要下沉的数
while(q<=hs_max)
{
if(q<hs_max&&heap_max[q+1]>heap_max[q]) q++;
if(heap_max[q]<=a) break;
heap_max[p]=heap_max[q];
p=q;
q=p<<1;
}
heap_max[p]=a;
}
int del_max()//去最小值,并且删除最小值
{
int r=heap_max[1];
heap_max[1]=heap_max[hs_max--];
sink_max(1);
return r;
}
void swim_max(int p)//上浮操作
{
int q=p>>1;//q是p的父亲节点
int a=heap_max[p];//要上浮的数
while(q&&a>heap_max[q])//如果比父亲节点小
{
heap_max[p]=heap_max[q];//上浮
p=q;q=p>>1;
}
heap_max[p]=a;
}
void insert_max(int a)//插入元素
{
heap_max[++hs_max]=a;
swim_max(hs_max);
}
void init_max()
{
hs_max=0;
}
int main()
{
int n1,n2,n;
while(scanf("%d%d%d",&n1,&n2,&n)==3&&n)
{
double cnt=0;
init_max();//最大堆,求前n2小的数
init_min();//最小堆,求前n1大的数
for(int i=1;i<15;i++) insert_max(inf),insert_min(-inf);
for(int i=0;i<n;i++)
{
int x;scanf("%d",&x);
cnt+=x;
if(x<heap_max[1])
{
del_max();
insert_max(x);
}
if(x>heap_min[1])
{
del_min();
insert_min(x);
}
}
int x[15];
for(int i=1;i<15;i++)
{
x[i]=del_max();
}
for(int i=14,j=0;i>=1&&j<n2;i--,j++) cnt-=x[i];
for(int i=1;i<15;i++)
{
x[i]=del_min();
}
for(int i=14,j=0;i>=1&&j<n1;i--,j++) cnt-=x[i];
printf("%lf/n",cnt/(n-n1-n2));
}
return 0;
}