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 ...
随机推荐
- IOS 手机端搜索硬件设备 --- 物联网
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #i ...
- 使用Spring JDBCTemplate简化JDBC的操作
使用Spring JDBCTemplate简化JDBC的操作 接触过JAVA WEB开发的朋友肯定都知道Hibernate框架,虽然不否定它的强大之处,但个人对它一直无感,总感觉不够灵活,太过臃肿了. ...
- 常见maven镜像
国内连接maven官方的仓库更新依赖库,网速一般很慢,收集一些国内快速的maven仓库镜像以备用. ====================国内OSChina提供的镜像,非常不错=========== ...
- (转) class II
Overloading operators Classes, essentially, define new types to be used in C++ code. And types in ...
- Wordpress更换主题之后出错
今天吃完午饭,休息休息,最近搞了一下google adsense,不过最终的审核没通过,我想会不会是界面不好看呢,饭后就在电脑旁,更换了几个wordpress主题,我的博客使用wordpress搭建的 ...
- Tango_with_django_17笔记
1. 在update Category tabole with SlugField中,起因是url会将空格自动转换成%20,为了把url变得美观,易读,可以用slugify,它可以把空格替换为连字符( ...
- Js 导出Excel IE ActiveX控件
function ExportExcel() { var oXL = new ActiveXObject("Excel.Application"); //创建excel应用程序对象 ...
- sql server中主键列的插入问题
仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'dbo.t_test'中的标识列指定显式值. SET IDENTITY_INSERT dbo.t_test ON ,'c' ...
- nginx+keepalived+tomcat之tomcat性能调优
body{ font-family: Nyala; font-size: 10.5pt; line-height: 1.5;}html, body{ color: ; background-color ...
- Python学习(七) 流程控制if语句
在Python中流程控制if语句采用如下格式: if expression : statement elif expression : statement elif expression : stat ...