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. 1 2 5
  2. 1 2 3 4 5
  3. 4 2 10
  4. 2121187 902 485 531 843 582 652 926 220 155
  5. 0 0 0

Sample Output

  1. 3.500000
  2. 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.

开始觉得这个题很简单,直接用优先队列排序即可,显然我没有注意到该题的数据非常大,内存要求又很严格

所以刚开始把所有数据都存入优先队列中的方法太占用内存

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<queue>
  4. using namespace std;
  5. int main()
  6. {
  7. int n,n1,n2;
  8. double t,s;
  9. while(scanf("%d%d%d",&n1,&n2,&n)==){
  10. if(n==)break;
  11. priority_queue<double>p;
  12. s=;
  13. for(int i=;i<n;i++){
  14. cin>>t;
  15. p.push(t);
  16. }
  17. for(int i=;i<n-n2;i++){
  18. if(i<n1)p.pop();
  19. else {
  20. s+=p.top();
  21. p.pop();
  22. }
  23. }
  24. printf("%.6f\n",s/(n-n1-n2));
  25. }
  26. return ;
  27. }

然后将数据类型改为float  显然也不行,数据太大导致溢出

因此必然将用另一种方法来做,注意到n1与n2都很小,所以分别使用两个优先队列来存贮n1个最大值和n2个最小值

以下代码为具体的思路,然而超时,再修改!!

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<queue>
  4. using namespace std;
  5. int main()
  6. {
  7. int n,n1,n2;
  8. priority_queue<long double>small,big;
  9. long double t,s;
  10. while(scanf("%d%d%d",&n1,&n2,&n)==&&n){
  11. s=;
  12. for(int i=;i<n;i++){
  13. scanf("%lf",&t);
  14. s+=t;
  15. small.push(t);
  16. if(small.size()>n2)small.pop();
  17. big.push(-t);
  18. if(big.size()>n1)big.pop();
  19. }
  20. for(int i=;i<n1;i++){
  21. s+=big.top();
  22. big.pop();
  23. }
  24. for(int i=;i<n2;i++){
  25. s-=small.top();
  26. small.pop();
  27. }
  28. printf("%.6lf\n",s*1.0/(n-n1-n2));
  29. }
  30. return ;
  31. }
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<queue>
  4. using namespace std;
  5. int main()
  6. {
  7. int n,n1,n2;
  8. priority_queue<long double>small,big;
  9. long double t,s;
  10. while(scanf("%d%d%d",&n1,&n2,&n)==&&n){
  11. s=;
  12. for(int i=;i<n;i++){
  13. scanf("%lf",&t);
  14. s+=t;
  15. small.push(t);
  16. if(small.size()>n2)small.pop();
  17. big.push(-t);
  18. if(big.size()>n1)big.pop();
  19. }
  20. for(int i=;i<n1;i++){
  21. s+=big.top();
  22. big.pop();
  23. }
  24. for(int i=;i<n2;i++){
  25. s-=small.top();
  26. small.pop();
  27. }
  28. printf("%.6lf\n",s/(n-n1-n2));
  29. }
  30. return ;
  31. }

将上述t及s的数据类型改为long long,代码竟然奇迹般的通过了

说明处理不同的数据类型,所用的时间是不同的,而long long的处理时间明显小于long double

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<queue>
  4. using namespace std;
  5. int main()
  6. {
  7. int n,n1,n2;
  8. priority_queue<long long>small,big;
  9. long long t,s;
  10. while(scanf("%d%d%d",&n1,&n2,&n)==&&n){
  11. s=;
  12. for(int i=;i<n;i++){
  13. scanf("%lld",&t);
  14. s+=t;
  15. small.push(t);
  16. if(small.size()>n2)small.pop();
  17. big.push(-t);
  18. if(big.size()>n1)big.pop();
  19. }
  20. for(int i=;i<n1;i++){
  21. s+=big.top();
  22. big.pop();
  23. }
  24. for(int i=;i<n2;i++){
  25. s-=small.top();
  26. small.pop();
  27. }
  28. printf("%.6f\n",s*1.0/(n-n1-n2));
  29. }
  30. return ;
  31. }

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. (转) Overloads and templates

    Overloaded functions In C++, two different functions can have the same name if their parameters are ...

  2. NYOJ 45 棋盘覆盖

    棋盘覆盖 水题,题不难,找公式难 import java.math.BigInteger; import java.util.Scanner; public class Main { public s ...

  3. ORACLE11G常用函数

    1 单值函数 1.1 日期函数 1.1.1 Round [舍入到最接近的日期](day:舍入到最接近的星期日) select sysdate S1, round(sysdate) S2 , round ...

  4. 用Apache Ivy实现项目里的依赖管理

    Apache Ivy是一个管理项目依赖的工具. 它与Maven  Apache Maven 构建管理和项目管理工具已经吸引了 Java 开发人员的注意.Maven 引入了 JAR 文件公共存储库的概念 ...

  5. < meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" />的作用

    < meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" /> 介绍:这 ...

  6. Javascript经典实例 - 字符串

    1] 'this is a string'这是字符串直接量,new String('this is a string')这是字符串对象,字符串对象可以用字符串对象所带的属性和方法,直接量在“表面上”也 ...

  7. 容器的深入研究(二)—Set与Map

    一.Set类的作用 二.Set类延生的四种形式 三.非基础类型如何使用Set的四种形式 四.Queue的使用 五.PriorityQueue的使用 六.Map的六种形式 七.HashMap散列码的实现 ...

  8. linux多线程编程之互斥锁

    多线程并行运行,共享同一种互斥资源时,需要上互斥锁来运行,主要是用到pthread_mutex_lock函数和pthread_mutex_unlock函数对线程进行上锁和解锁 下面是一个例子: #in ...

  9. 如何使用Prism框架的EventAggregator在模块间进行通信

    目的 本文主要介绍如何使用Prism类库提供的事件机制在松耦合组件之间相互通信,Prism类库的事件机制建立在事件聚合服务之上,允许发布者和订阅者通过事件进行通信,不需要彼此之间引用. 事件聚合 Ev ...

  10. 报LinkageError的原因

    LinkageError是一个比较棘手的异常,准确的说它是一个Error而不是Exception.java api对它没有直接的解释,而是介绍了它的子类: Subclasses of LinkageE ...