W - stl 的 优先队列 Ⅲ
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.
开始觉得这个题很简单,直接用优先队列排序即可,显然我没有注意到该题的数据非常大,内存要求又很严格
所以刚开始把所有数据都存入优先队列中的方法太占用内存
#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)==){
if(n==)break;
priority_queue<double>p;
s=;
for(int i=;i<n;i++){
cin>>t;
p.push(t);
}
for(int i=;i<n-n2;i++){
if(i<n1)p.pop();
else {
s+=p.top();
p.pop();
}
}
printf("%.6f\n",s/(n-n1-n2));
}
return ;
}
然后将数据类型改为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)==&&n){
s=;
for(int i=;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=;i<n1;i++){
s+=big.top();
big.pop();
}
for(int i=;i<n2;i++){
s-=small.top();
small.pop();
}
printf("%.6lf\n",s*1.0/(n-n1-n2));
}
return ;
}
#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)==&&n){
s=;
for(int i=;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=;i<n1;i++){
s+=big.top();
big.pop();
}
for(int i=;i<n2;i++){
s-=small.top();
small.pop();
}
printf("%.6lf\n",s/(n-n1-n2));
}
return ;
}
将上述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)==&&n){
s=;
for(int i=;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=;i<n1;i++){
s+=big.top();
big.pop();
}
for(int i=;i<n2;i++){
s-=small.top();
small.pop();
}
printf("%.6f\n",s*1.0/(n-n1-n2));
}
return ;
}
W - stl 的 优先队列 Ⅲ的更多相关文章
- STL之优先队列
STL 中优先队列的使用方法(priority_queu) 基本操作: empty() 如果队列为空返回真 pop() 删除对顶元素 push() 加入一个元素 size() 返回优先队列中拥有的元素 ...
- 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动
一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...
- STL中优先队列的使用
普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出的行为特征.我们来说一下C++的 ...
- STL priority_queue 优先队列 小记
今天做题发现一个很有趣的地方,竟然还是头一次发现,唉,还是太菜了. 做图论用STL里的priority_queue去优化prim,由于特殊需求,我需要记录生成树中是用的哪些边. 于是,我定义的优先队列 ...
- STL之优先队列(1)
优先队列用法 在优先队列中,优先级高的元素先出队列. 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系. 优先队列的第一种用法: 也是最常用的用法 priority_queue< ...
- STL之优先队列(priority_queue)
转自网上大牛博客,原文地址:http://www.cnblogs.com/summerRQ/articles/2470130.html 先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对 ...
- V - stl 的 优先队列 Ⅱ
Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every morni ...
- hdu 4393 Throw nails(STL之优先队列)
Problem Description The annual school bicycle contest started. ZL is a student in this school. He is ...
- hdu1716排列2(stl:next_permutation+优先队列)
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
随机推荐
- VLD 1.0 ReadMe翻译尝试
近期想学习下VLD的实现,打算从最简单的V1.0版本看起.以下是V1.0版本自己尝试翻译下,最新的2.x版本似乎强大了很多. 简介 Visual C++提供了内置的内存检测机制,但其充其量只满足了最小 ...
- Ubuntu14.04上安装tftpd服务
首先sudo apt-get install tftpd-hpa, tftp-hpa 然后sudo vim /etc/default/tftpd-hpa 配置文件如下 TFTP_USERNAME=& ...
- build opencv with python support
cmake -DPYTHON_LIBRARY=/opt/anaconda/lib/libpython2.7.so -DPYTHON_INCLUDE_DIR=/opt/anaconda/include/ ...
- 实现TCP断点上传,后台C#服务实现接收
实现TCP断点上传,后台C#服务实现接收 终端实现大文件上传一直都是比较难的技术,其中涉及到后端与前端的交互,稳定性和流量大小,而且实现原理每个人都有自己的想法,后端主流用的比较多的是Http来实现, ...
- [TYVJ] P1010 笨小猴
笨小猴 背景 Background NOIP2008复赛提高组第一题 描述 Description 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种 ...
- C# 集合性能比较(代码测试)
using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...
- Linux系统编程(5)——文件与IO之mmap函数
mmap系统调用它本身提供了不同于一般对普通文件的访问方式,进程可以像读写内存一样对普通文件的操作.而Posix或系统V的共享内存IPC则纯粹用于共享目的,mmap()实现共享内存也是其主要应用之一. ...
- Eclipse中设置注释、日期等信息
在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的,例如其中author,datetime等属性值. 我们可以在Eclipse 中进行设置自己希望显示的注释信息 ...
- 剑指offer-面试题6.重建二叉树
题目:输入某二叉树的前序遍历和中序遍历结果,请重建出该二叉树.假设 输入的前序遍历和中序遍历的结果都不含重复的数字.例如输入前序遍历 序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...
- vhd镜像格式及vhd-util工具应用
概述 VHD 是微软虚拟磁盘文件格式,qemu和tapdisk2都能支持VHD镜像格式,不仅提供基本的虚拟机卷功能,还可提供磁盘快照.磁盘扩容等高级功能 vhd-util是个linux下的开源软件,通 ...