The Average
Time Limit: 6000MS | Memory Limit: 10000K | |
Total Submissions: 9527 | Accepted: 2961 | |
Case Time Limit: 4000MS |
Description
In a speech contest, when a contestantfinishes his speech, the judges will then grade his performance. The staffremove the highest grade and the lowest grade and compute the average of therest as the contestant’s final grade. This is an easy problem because usuallythere are only several judges.
Let’s consider a generalized form of theproblem above. Given n positive integers, remove the greatest n1 onesand the least n2 ones, and compute the average ofthe 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 npositiveintegers 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 averagerounded 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 220155
0 0 0
Sample Output
3.500000
562.500000
Hint
This problem has very large input data. scanf and printf arerecommended for C++ I/O.
The memory limit might not allow you tostore everything in the memory.
C:
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[5000001];
bool cmp(int a,int b)
{
if(a>b)
returnfalse;
elsereturn true;
}
int main()
{
intn1,n2,n,x,i;
while(scanf("%d%d%d",&n1,&n2,&n)&&(n1!=0||n2!=0||n!=0))
{
__int64sum=0;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);
for(i=n2;i<n-n1;i++)
sum+=a[i];
printf("%.6lf\n",1.0*sum/(n-n1-n2));
}
return0;
}
以上代码提交后出现错误:Memory Limit Exceeded
因为一次最多可以输入5000000个数,数据输入量过大,不能一次读入。若一次性读入,就会出现内存不够用的情况。所以应该采取输入一个数处理一个数的办法。
当然此道题很显然涉及排序,以下代码是采用sort()排序:
#include<stdio.h>
#include<algorithm>
usingnamespace std;
boolcmp(int a,int b)
{
if(a>b)
return true;
else return false;
}
intmain()
{
int n1,n2,n,x,i;
int high[11],low[11];
while(scanf("%d%d%d",&n1,&n2,&n)&&(n1!=0||n2!=0||n!=0))
{
fill(high,high+11,0);
fill(low,low+11,20000000);
__int64 sum=0;
for(i=0;i<n;i++)
{
scanf("%d",&x);
sum+=x;
if(x>high[n1])
{
high[n1]=x;
sort(high,high+n1+1,cmp);
}
if(x<low[n2])
{
low[n2]=x;
sort(low,low+n2+1);
}
}
for(i=0;i<n1;i++)
sum-=high[i];
for(i=0;i<n2;i++)
sum-=low[i];
printf("%.6lf\n",1.0*sum/(n-n1-n2));
}
return 0;
}
以下代码采用堆排序:
#include<stdio.h>
#include<algorithm>
usingnamespace std;
boolcmp(int a,int b)
{
if(a>b)
return true;
else return false;
}
intmain()
{
int n1,n2,n,x,i;
int high[11],low[11];
while(scanf("%d%d%d",&n1,&n2,&n)&&(n1!=0||n2!=0||n!=0))
{
fill(high,high+12,0);
fill(low,low+12,20000000);
__int64 sum=0;
for(i=0;i<n;i++)
{
scanf("%d",&x);
sum+=x;
if(x>high[n1])
{
high[n1]=x;
make_heap(high,high+n1+1,cmp);
pop_heap(high,high+n1+1,cmp);
}
if(x<low[n2])
{
low[n2]=x;
make_heap(low,low+n2+1);
pop_heap(low,low+n2+1);
}
}
for(i=0;i<n1;i++)
sum-=high[i];
for(i=0;i<n2;i++)
sum-=low[i];
printf("%.6lf\n",1.0*sum/(n-n1-n2));
}
return 0;
}