W

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 ≤ in) 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<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
    int n,n1,n2;
    double t,s;
    while(scanf("%d%d%d",&n1,&n2,&n)==3){
        if(n==0)break;
        priority_queue<double>p;
        s=0;
        for(int i=0;i<n;i++){
            cin>>t;
            p.push(t);
        }
        for(int i=0;i<n-n2;i++){
            if(i<n1)p.pop();
            else {
                s+=p.top();
                p.pop();
            }
        }
        printf("%.6f
",s/(n-n1-n2));
    }
    return 0;
}

然后将数据类型改为float  显然也不行,数据太大导致溢出

因此必然将用另一种方法来做,注意到n1与n2都很小,所以分别使用两个优先队列来存贮n1个最大值和n2个最小值

以下代码为具体的思路,然而超时,再修改!!

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
    int n,n1,n2;
    priority_queue<long double>small,big;
    long double t,s;
    while(scanf("%d%d%d",&n1,&n2,&n)==3&&n){
        s=0;
        for(int i=0;i<n;i++){
            scanf("%lf",&t);
            s+=t;
            small.push(t);
            if(small.size()>n2)small.pop();
            big.push(-t);
            if(big.size()>n1)big.pop();
        }
        for(int i=0;i<n1;i++){
            s+=big.top();
            big.pop();
        }
        for(int i=0;i<n2;i++){
            s-=small.top();
            small.pop();
        }
        printf("%.6lf
",s*1.0/(n-n1-n2));
    }
    return 0;
}
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
    int n,n1,n2;
    priority_queue<long double>small,big;
    long double t,s;
    while(scanf("%d%d%d",&n1,&n2,&n)==3&&n){
        s=0;
        for(int i=0;i<n;i++){
            scanf("%lf",&t);
            s+=t;
            small.push(t);
            if(small.size()>n2)small.pop();
            big.push(-t);
            if(big.size()>n1)big.pop();
        }
        for(int i=0;i<n1;i++){
            s+=big.top();
            big.pop();
        }
        for(int i=0;i<n2;i++){
            s-=small.top();
            small.pop();
        }
        printf("%.6lf
",s/(n-n1-n2));
    }
    return 0;
}

将上述t及s的数据类型改为long long,代码竟然奇迹般的通过了

说明处理不同的数据类型,所用的时间是不同的,而long long的处理时间明显小于long double

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
    int n,n1,n2;
    priority_queue<long long>small,big;
    long long t,s;
    while(scanf("%d%d%d",&n1,&n2,&n)==3&&n){
        s=0;
        for(int i=0;i<n;i++){
            scanf("%lld",&t);
            s+=t;
            small.push(t);
            if(small.size()>n2)small.pop();
            big.push(-t);
            if(big.size()>n1)big.pop();
        }
        for(int i=0;i<n1;i++){
            s+=big.top();
            big.pop();
        }
        for(int i=0;i<n2;i++){
            s-=small.top();
            small.pop();
        }
        printf("%.6f
",s*1.0/(n-n1-n2));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/farewell-farewell/p/5253972.html