博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
W - stl 的 优先队列 Ⅲ
阅读量:6613 次
发布时间:2019-06-24

本文共 3278 字,大约阅读时间需要 10 分钟。

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 51 2 3 4 54 2 102121187 902 485 531 843 582 652 926 220 1550 0 0

Sample Output

3.500000562.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
#include
#include
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
p; s=0; for(int i=0;i
>t; p.push(t); } for(int i=0;i

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

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

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

#include
#include
#include
using namespace std;int main(){ int n,n1,n2; priority_queue
small,big; long double t,s; while(scanf("%d%d%d",&n1,&n2,&n)==3&&n){ s=0; for(int i=0;i
n2)small.pop(); big.push(-t); if(big.size()>n1)big.pop(); } for(int i=0;i
#include
#include
#include
using namespace std;int main(){ int n,n1,n2; priority_queue
small,big; long double t,s; while(scanf("%d%d%d",&n1,&n2,&n)==3&&n){ s=0; for(int i=0;i
n2)small.pop(); big.push(-t); if(big.size()>n1)big.pop(); } for(int i=0;i

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

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

#include
#include
#include
using namespace std;int main(){ int n,n1,n2; priority_queue
small,big; long long t,s; while(scanf("%d%d%d",&n1,&n2,&n)==3&&n){ s=0; for(int i=0;i
n2)small.pop(); big.push(-t); if(big.size()>n1)big.pop(); } for(int i=0;i

 

转载地址:http://fxaso.baihongyu.com/

你可能感兴趣的文章
[20170625]参数LOG_ARCHIVE_DEST_1.txt
查看>>
迟来的加勒比海盗3 观后
查看>>
从头开始学JavaScript (十二)——Array类型
查看>>
重构——43添加参数(Add Parameter)
查看>>
类与对象 - PHP手册笔记
查看>>
谈一谈互联网创业补贴变味后的现象
查看>>
押宝在Apple Watch的智能手表游戏玩得转吗?
查看>>
MapGIS转Shp文件的单位问题
查看>>
Android零基础入门第42节:自定义BaseAdapter
查看>>
分析非结构化数据的10个步骤
查看>>
Python——循环
查看>>
串口传输文件 lrzsz
查看>>
网站架构的伸缩性设计
查看>>
谈谈Spring boot 启动层面的开发
查看>>
众说纷纭:NVMe over Fabrics阵列到底属不属于SAN?
查看>>
三个案例透析大数据思维的核心
查看>>
Linux 4.9内核终被“扶正”,落实说好的LTS待遇
查看>>
物联网时代的八大工业大数据应用场景
查看>>
数据库索引的实现原理
查看>>
Ovum观察:SDN/NFV大幅缩短新兴市场运营商服务上市时间
查看>>