您的位置:首页 > 编程语言 > PHP开发

poj -- 2833 The Average(优先队列或堆或直接模拟)

2011-04-10 16:49 405 查看
http://poj.org/problem?id=2833

 

 

The AverageTime Limit: 6000MS Memory Limit: 10000K

Total Submissions: 7443 Accepted: 2288

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.

 

题目限制了内存。但是题目给的n1和n2很小

 

所以用优先队列或堆来维护2个最大和最小值的数组

 

用了3个方法,堆的效率最高。

 

 

1.直接模拟

 

//Memory: 160K		Time: 3766MS

#include <cstdio>
#include <algorithm>

using namespace std;
__int64 max_t[11],min_t[11],total,data;

int main()
{
int i,n1,n2,n;
double ave;
while(1)
{
memset(max_t,0,sizeof(max_t));
memset(min_t,100000001,sizeof(min_t));//初始化数组
total = 0;
scanf("%d%d%d",&n1,&n2,&n);
if(n1+n2+n == 0)
break;
for(i = 0;i < n;i ++)
{
scanf("%I64d",&data);
total = total+data;//求总和
sort(max_t,max_t+n1);//对存储最大值的数组排序
sort(min_t,min_t+n2);//对存储最小值的数组排序
if(data > max_t[0])//与最大值数组中的最小值比较
max_t[0] = data;
if(min_t[n2-1] > data)//与最小值数组中的最大值比较
min_t[n2-1] = data;
}
for(i = 0;i < n1;i ++)
total = total - max_t[i];
for(i = 0;i < n2;i ++)
total = total - min_t[i];
ave = total/(double)(n-n1-n2);//求平均值
printf("%.6lf/n",ave);
}
return 0 ;
}


 

 

2.队列

 

//Memory: 176K		Time: 3297MS

#include <iostream>
#include <queue>
using namespace std;
int Max[15],Min[15];
int s1,s2;
int cmp(int a,int b)
{
return a>b;
}
int main()
{
int n1,n2,n,i,x;
__int64 sum;
while (scanf("%d%d%d",&n1,&n2,&n)&&(n1+n2+n))
{
priority_queue <int>q1;		//从大到小。存小数
priority_queue <int,vector<int>,greater<int> >q2;
n1++,n2++;
sum=0;
for (i=0;i<n;i++)
{
scanf("%d",&x);
sum+=x;
q2.push(x);
q1.push(x);
if(q1.size()>n2)
q1.pop();
if(q2.size()>n1)
q2.pop();
}
q1.pop();
q2.pop();
while(!q1.empty())
{
sum-=q1.top();
q1.pop();
}
while(!q2.empty())
{
sum-=q2.top();
q2.pop();
}
n-=(n1+n2-2);
printf("%.6lf/n",sum/(n+0.0));
}
return 0;
}


 

3.堆

 

用法 :http://blog.csdn.net/zsc09_leaf/archive/2011/04/10/6313716.aspx

 

//Memory: 160K		Time: 2579MS

#include <iostream>
#include <algorithm>
using namespace std;
int Max[15],Min[15];
int s1,s2;
int cmp(int a,int b)
{
return a>b;
}
int main()
{
int n1,n2,n,i,x;
double sum;
while (scanf("%d%d%d",&n1,&n2,&n)&&(n1+n2+n))
{
sum=0;
for (i=0;i<11;i++)
{
Max[i]=0;
Min[i]=2000000000;
}
for(i=0;i<n;i++)
{
scanf("%d",&x);
sum+=x;
if(x>Max[n1])
{
Max[n1]=x;
make_heap(Max,Max+n1+1,cmp);
pop_heap(Max,Max+n1+1,cmp);
}
if(x<Min[n2])
{
Min[n2]=x;
make_heap(Min,Min+n2+1);
pop_heap(Min,Min+n2+1);
}
}
for(i=0;i<n1;i++)
sum-=Max[i];
for (i=0;i<n2;i++)
sum-=Min[i];
n-=(n1+n2);
printf("%.6lf/n",sum/(n+0.0));
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  n2 input output each 存储