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 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 的 优先队列 Ⅲ的更多相关文章

  1. STL之优先队列

    STL 中优先队列的使用方法(priority_queu) 基本操作: empty() 如果队列为空返回真 pop() 删除对顶元素 push() 加入一个元素 size() 返回优先队列中拥有的元素 ...

  2. 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动

    一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...

  3. STL中优先队列的使用

    普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出的行为特征.我们来说一下C++的 ...

  4. STL priority_queue 优先队列 小记

    今天做题发现一个很有趣的地方,竟然还是头一次发现,唉,还是太菜了. 做图论用STL里的priority_queue去优化prim,由于特殊需求,我需要记录生成树中是用的哪些边. 于是,我定义的优先队列 ...

  5. STL之优先队列(1)

    优先队列用法 在优先队列中,优先级高的元素先出队列. 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系. 优先队列的第一种用法: 也是最常用的用法 priority_queue< ...

  6. STL之优先队列(priority_queue)

    转自网上大牛博客,原文地址:http://www.cnblogs.com/summerRQ/articles/2470130.html 先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对 ...

  7. V - stl 的 优先队列 Ⅱ

    Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every morni ...

  8. hdu 4393 Throw nails(STL之优先队列)

    Problem Description The annual school bicycle contest started. ZL is a student in this school. He is ...

  9. hdu1716排列2(stl:next_permutation+优先队列)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

随机推荐

  1. XtraReport改变纸张方向

    XtraReport纸张方向改变可以通过修改Landscape属性: Landscape=true 为横向输出 Landscape=false 为纵向输出

  2. iOS 之 cocoapods安装与使用

    我们都知道第三方库,一般使用cocoapods管理,cocoapods在我们IOS开发中有着很大的作用. 好了,现在看下它的安装步骤: 1.打开终端,输入 sudo gem install cocoa ...

  3. 从汇编看c++中含有虚基类对象的析构

    c++中,当继承结构中含有虚基类时,在构造对象时编译器会通过将一个标志位置1(表示调用虚基类构造函数),或者置0(表示不调用虚基类构造函数)来防止重复构造虚基类子对象.如下图菱形结构所示: 当构造类B ...

  4. Java 一个字符串在另外一个字符串出现次数

    统计一个字符串在另外一个字符串出现次数 代码如下: package me.chunsheng.javatest; import java.util.regex.Matcher; import java ...

  5. Linux释放内存

    在Linux系统下,我们一般不需要去释放内存,因为系统已经将内存管理的很好.但是凡事也有例外,有的时候内存会被缓存占用掉,导致系统使用SWAP空间影响性能,此时就需要执行释放内存(清理缓存)的操作了. ...

  6. C和指针c6-1

    #include<stdio.h> #include<stdlib.h> char *find_char(char const *source_str, char const ...

  7. HttpClient post中文乱码解决

    在javase方式下使用HttpClient没有进行任何编码设置,本地从服务端获取到数据不存在中文乱码. 但是将此段代码部署到Tomcat下面出现了中文乱码,此时设置: post.getParams( ...

  8. 360云后台(使用HTTP Cache服务器)

    工作职责:设计优化HTTP Cache服务器,负载均衡服务器,调度系统等核心系统开发优化CDN系统架构满足流量.性能.成本要求 技能要求: 精通Linux, C/C++语言,HTTP协议精通高性能服务 ...

  9. jQuery多版本的使用,同一文件多个版本引用

    <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  10. codecomb 2086【滑板鞋】

    题目背景 我的滑板鞋时尚时尚最时尚 回家的路上我情不自禁 摩擦 摩擦 在这光滑的地上摩擦 月光下我看到自己的身影有时很远有时很近 感到一种力量驱使我的脚步 有了滑板鞋天黑都不怕 题目描述 你在魅力之都 ...