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 ...
随机推荐
- (原)Eclipse中将JNI生成的so打包成jar的步骤
说明:新人,对java不熟,见谅. 1. 新建工程,添加好native support,写好对应的文件(包括cpp文件,so文件和对应的java文件,此处称对应的java文件为SoJAR.java,) ...
- [转]fatal error: iostream.h: No such file or directory
iostream.h是非标准头文件,iostream是标准头文件形式.iostream.h时代没有名词空间,即所有库函数包括头文件iostream.h都声明在全局域.为了体现结构层次,c++标准委员会 ...
- PHP 中 const define 的区别
在php中定义常量时,可用到const与define这两种方法,那他们到底有什么区别呢? 1.const用于类成员变量的定义,一经定义,不可修改.define不可用于类成员变量的定义,可用于全局常量. ...
- python笔记之hashlib模块
涉及加密服务:14. Cryptographic Services其中 hashlib是涉及安全散列和消息摘要,提供多个不同的加密算法借口,如SHA1.SHA224.SHA256.SHA384.SHA ...
- Java程序员需要学习的知识点
Java是全世界最受欢迎的3大编程语言之一,它可以开发出许多实用的WEB应用程序和桌面应用程序,更重要的一点,Java是跨平台的语言——编写一次,可以再任何地方运行.另外,Java也很容易入门,如果你 ...
- log4j 将日志记录到数据库
需要以下jar包: ---log4j commons-loggin-1.1.1.jar log4j-1.2.16.jar ---mysql mysql-connector-java-5.1.15-bi ...
- Keil中Memory Model和Code Rom Size说明
C51中定义变量时如果省略存储器类型,Keil C51编译系统则会按编译模式SMALL.COMPACT和LARGE所规定的默认存储器类型去指定变量的存储区域,无论什么存储模式都可以声明变量在任何的80 ...
- QTabWidget and QTabBar.的文字的颜色设置,三种方法
see the code after subclassingTabWidget::TabWidget(QWidget *parent): QTabWidget(parent),mousePressFl ...
- SQL Server索引 - 聚集索引、非聚集索引、非聚集唯一索引 <第八篇>
聚集索引.非聚集索引.非聚集唯一索引 我们都知道建立适当的索引能够提高查询速度,优化查询.先说明一下,无论是聚集索引还是非聚集索引都是B树结构. 聚集索引默认与主键相匹配,在设置主键时,SQL Ser ...
- 文档生成工具doxygen+图像生成工具GraphViz
文档生成工具doxygen+图像生成工具GraphViz 虽然jdk自带的javadoc也很好用,不过使用doxygen+GraphViz 的组合可以生成许多强大的图(类图.协作图.文件包含/被包含图 ...