2833 The Average 求数组的前k小、大数 最大堆,最小堆 STL

这是一个程序实现的问题,旨在解决从一组n个正整数中,去除n1个最大值和n2个最小值后,剩余数值的平均值。程序通过建立最大堆来找到n2个最小值,通过最小堆找到n1个最大值,然后计算剩余数值的平均值。输入包括n1, n2, n和n个正整数,输出是经过处理后的平均值,保留六位小数。" 128536486,11915645,Docker深度解析:命令使用与实战应用,"['Docker', '容器', '运维', '服务编排', '数据管理']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Average
Time Limit: 6000MS Memory Limit: 10000K
Total Submissions: 7095 Accepted: 2184
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 ≤ 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<cstring>
#include<algorithm>
using namespace std;
const int inf=(1<<30);
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int n1,n2,n;
    while(scanf("%d%d%d",&n1,&n2,&n)==3&&n)
    {
        int _max[15],_min[15];
        for(int i=0;i<15;i++) _max[i]=inf,_min[i]=-inf;
        make_heap(_max,_max+15);//最大堆,求前n2小的数
        make_heap(_min,_min+15,cmp);//最小堆,求前n1大的数
        double cnt=0;
        for(int i=0;i<n;i++)
        {
            int x;scanf("%d",&x);
            cnt+=x;
            if(x<_max[0])
            {
                pop_heap(_max,_max+15);
                _max[14]=x;
                push_heap(_max,_max+15);

            }
            if(x>_min[0])
            {
                pop_heap(_min,_min+15,cmp);
                _min[14]=x;
                push_heap(_min,_min+15,cmp);
            }
        }
        int x[15];
        for(int i=0;i<15;i++)
        {
            x[i]=_max[0];
            pop_heap(_max,_max+15-i);
        }
        for(int i=14,j=0;i>=0&&j<n2;i--,j++) cnt-=x[i];
        for(int i=0;i<15;i++)
        {
            x[i]=_min[0];
            pop_heap(_min,_min+15-i,cmp);
        }
        for(int i=14,j=0;i>=0&&j<n1;i--,j++) cnt-=x[i];
        printf("%lf/n",cnt/(n-n1-n2));
    }
    return 0;
}

 

 

 

 

 

 

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int inf=(1<<30);
int heap_max[15],heap_min[15];
int hs_max,hs_min;
//最小堆,从1开始
void sink_min(int p)//下沉操作
{
    int q=p<<1;//q是p的左儿子节点
    int a=heap_min[p];//要下沉的数
    while(q<=hs_min)
    {
        if(q<hs_min&&heap_min[q+1]<heap_min[q]) q++;
        if(heap_min[q]>=a) break;
        heap_min[p]=heap_min[q];
        p=q;
        q=p<<1;
    }
    heap_min[p]=a;
}
int del_min()//去最小值,并且删除最小值
{
    int r=heap_min[1];
    heap_min[1]=heap_min[hs_min--];
    sink_min(1);
    return r;
}
void swim_min(int p)//上浮操作
{
    int q=p>>1;//q是p的父亲节点
    int a=heap_min[p];//要上浮的数
    while(q&&a<heap_min[q])//如果比父亲节点小
    {
        heap_min[p]=heap_min[q];//上浮
        p=q;q=p>>1;
    }
    heap_min[p]=a;
}
void insert_min(int a)//插入元素
{
    heap_min[++hs_min]=a;
    swim_min(hs_min);
}
void init_min()
{
    hs_min=0;
}
//最大堆,从1开始
void sink_max(int p)//下沉操作
{
    int q=p<<1;//q是p的左儿子节点
    int a=heap_max[p];//要下沉的数
    while(q<=hs_max)
    {
        if(q<hs_max&&heap_max[q+1]>heap_max[q]) q++;
        if(heap_max[q]<=a) break;
        heap_max[p]=heap_max[q];
        p=q;
        q=p<<1;
    }
    heap_max[p]=a;
}
int del_max()//去最小值,并且删除最小值
{
    int r=heap_max[1];
    heap_max[1]=heap_max[hs_max--];
    sink_max(1);
    return r;
}
void swim_max(int p)//上浮操作
{
    int q=p>>1;//q是p的父亲节点
    int a=heap_max[p];//要上浮的数
    while(q&&a>heap_max[q])//如果比父亲节点小
    {
        heap_max[p]=heap_max[q];//上浮
        p=q;q=p>>1;
    }
    heap_max[p]=a;
}
void insert_max(int a)//插入元素
{
    heap_max[++hs_max]=a;
    swim_max(hs_max);
}
void init_max()
{
    hs_max=0;
}
int main()
{
    int n1,n2,n;
    while(scanf("%d%d%d",&n1,&n2,&n)==3&&n)
    {
        double cnt=0;
        init_max();//最大堆,求前n2小的数
        init_min();//最小堆,求前n1大的数
        for(int i=1;i<15;i++) insert_max(inf),insert_min(-inf);
        for(int i=0;i<n;i++)
        {
            int x;scanf("%d",&x);
            cnt+=x;
            if(x<heap_max[1])
            {
                del_max();
                insert_max(x);
            }
            if(x>heap_min[1])
            {
                del_min();
                insert_min(x);
            }
        }
        int x[15];
        for(int i=1;i<15;i++)
        {
            x[i]=del_max();

        }
        for(int i=14,j=0;i>=1&&j<n2;i--,j++) cnt-=x[i];
        for(int i=1;i<15;i++)
        {
            x[i]=del_min();
        }
        for(int i=14,j=0;i>=1&&j<n1;i--,j++) cnt-=x[i];
        printf("%lf/n",cnt/(n-n1-n2));
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值