当前位置: 代码迷 >> 综合 >> poj 2833 The Average优先队列的应用
  详细解决方案

poj 2833 The Average优先队列的应用

热度:81   发布时间:2024-01-12 20:30:13.0

题目链接:

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;
}

 

  相关解决方案