poj 2833 The Average优先队列的应用

题目链接:

http://poj.org/problem?id=2833

The Average

Time Limit: 6000MS Memory Limit: 10000K
Total Submissions: 13247 Accepted: 3947
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.

Source

POJ Monthly--2006.05.28, zby03

题目大意:

给你n个值,去掉n1个最大值,去掉n2个最小值,求剩下的平均值

排序的话会超内存,需要用优先队列储存最小值与最大值

This is the code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{
    int ret=0, flag=0;
    char ch;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        ret = ch - '0';
    while((ch=getchar())>='0'&&ch<='9')
        ret=ret*10+(ch-'0');
    return flag ? -ret : ret;
}
int main()
{
    long long n1,n2,n;//n1最大值,n2最小值
	while(scanf("%lld%lld%lld",&n1,&n2,&n))
	{
		if(!n1&&!n2&&!n)
		    break;
        priority_queue< int, vector<int>, greater<int> > big;//小根堆储存最大值
        priority_queue< int, vector<int>, less<int> > small;//大根堆储存最小值
		long long sum=0;
		for(int i=0;i<n;++i)
	    {
	    	long long tem;
	    	scanf("%lld",&tem);
	    	sum+=tem;
	    	big.push(tem);
	    	if(big.size()>n1)//储存最大值
	    	    big.pop();
	    	small.push(tem);
	    	if(small.size()>n2)//储存最小值
	    	    small.pop();
	    }
	    while(!big.empty())
	    {
	    	sum-=big.top();
	    	big.pop();
	    }
	    while(!small.empty())
	    {
	    	sum-=small.top();
	    	small.pop();
	    }
	    n=n-n1-n2;
	    double ans=1.0*sum/n;
	    printf("%.6f\n",ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值