G - stl 的 優先隊列

stl 的 優先隊列

題目描述

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.

Iutput

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 <cstdio>
#include <queue>
using namespace std;
int main()
{
    priority_queue<int,vector<int>,greater<int> > q2;//從小到大
    priority_queue<int,vector<int>,less<int> > q1;//從大到小

    int a,b,n,i;
    long long int temp;
    while(scanf("%d%d%d",&a,&b,&n),a|b|n){
        double s=0;
        for(i=1;i<=n;i++){
            scanf("%I64d",&temp);
            s+=temp;

            q2.push(temp);
            if(q2.size()>a)
                q2.pop();

            q1.push(temp);
            if(q1.size()>b)
                q1.pop();

        }
        for(i=1;i<=a;i++){
            temp=q2.top();
            q2.pop();

            s-=temp;
        }


        for(i=1;i<=b;i++){
            temp=q1.top();
            q1.pop();

            s-=temp;
        }

        printf("%.6f\n",1.0*s/(n-a-b));
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章