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 ...
随机推荐
- 【转】研华Adam6060某段时间后无法连接的问题
配合乙方测试,需连接现场Adam模块.一段时间后发现模块无法连接,网上资料甚少,发现此贴,记录下.以前没有多客户端高频次(其实谈不上高)连接,没有考虑连接释放的问题.另外,官方Demo也没有释放连接. ...
- javascript延迟加载及异步(defer和async)
一直以来写代码的时候的常用习惯就是吧所有的js文件直接加载在文档的head标签里面,在写js文件的时候有时候获取一些文件对象的时候为空对象,这是由于文档结构还没有加载完,但是js文件已经加载完.也就是 ...
- Javascript中String、Array常用方法介绍
string和array作为javascript内置对象,其中许多方法无论是在开发过程中,还是在面试的时候都有机会被面试官问到,这里对经常用到的方法做一个介绍,这些方法都有过很多的实际应用场景,所以对 ...
- Html 编码 queryUrl = encodeURI(queryUrl);
Html 编码 queryUrl = encodeURI(queryUrl);
- Unix 环境高级编程 (APUE) 之 网络 IPC:套接字
一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字 . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级 ...
- Compiler Principles 语法分析
语法分析的两种思维方式:1:自顶向下分析 :从语法树的根部推下来一直推到需要确认的终结符号串为止:就是为了找到一个符号串的最左推导 自顶向下分析,因为文法有些是以非终结符开头的另外文法中还可能含有右部 ...
- 【转】内核编译时, 到底用make clean, make mrproper还是make distclean(转载)
原文网址:http://dongyulong.blog.51cto.com/1451604/449470 内核编译时, 到底用make clean, make mrproper还是make distc ...
- 大数据时代的数据存储,非关系型数据库MongoDB(一)
原文地址:http://www.cnblogs.com/mokafamily/p/4076954.html 爆炸式发展的NoSQL技术 在过去的很长一段时间中,关系型数据库(Relational Da ...
- 无序线性搜索(Unordered Linear Search)
假定有一个元素顺序情况不明的数组.这种情况如果我们要搜索一个元素就要遍历整个数组,才能知道这个元素是否在数组中. 这种方法要检查整个数组,核对每个元素.下面是算法实现: #include<std ...
- 产生n bit所有可能的序列
void binary(int n) { ) printf("%s\n",A); // Assume A is a global variable else { A[n-] = ' ...