题目链接:http://acm.swust.edu.cn/problem/1094/

Time limit(ms): 1000      Memory limit(kb): 32768
 

中位数(又称中值,英语:Median),统计学中的专有名词,代表一个样本、种群或概率分布中的一个数值,其可将数值集合划分为相等的上下两部分。对于有限的数集,可以通过把所有观察值高低排序后找出正中间的一个作为中位数。如果观察值有偶数个,则中位数不唯一,通常取最中间的两个数值的平均数作为中位数。

Description

多组输入

第一行:一个正整数N (0<N<1000000)
第二行:N个正整数。(0=<A[i]<2^30)

Input

每组数据先输出”Case X:”,X表示测试数据的编号,从1开始。

第二行输出N个数,第i个数对应数组前i个值的中位数。(精确到小数点后一位)

Output
1
2
3
4
5
5
1 2 3 4 5
6
2 5 4 8 7 4
 
Sample Input
1
2
3
4
5
Case 1:
1.0 1.5 2.0 2.5 3.0
Case 2:
2.0 3.5 4.0 4.5 5.0 4.5
 
Sample Output
输出换行请使用\r\n

Hint
swust第10届校赛
 
 
解题思路:由于数据量太大,那么解题关键就在排序上面了,那么可以巧用堆排序,或者是使用容器 multiset(允许存在相同元素),那么就不会超时了。
 
下面是巧用容器的代码
 
  1. #include <iostream>
  2. #include <set>
  3. #include <cstdio>
  4. using namespace std;
  5.  
  6. #define rep(i,a,b) for(int i=a;i<=b;i++)
  7. int t, n;
  8. multiset<double>mpt;
  9.  
  10. void mid_num(){
  11. double x;
  12. printf("Case %d:\r\n", ++t);
  13. mpt.clear();
  14. scanf("%lf", &x);
  15. mpt.insert(x);
  16. multiset <double> ::iterator it = mpt.begin();
  17. printf("%.1lf", x);
  18. rep(i, , n){
  19. scanf("%lf", &x);
  20. mpt.insert(x);
  21. if (x < *it){
  22. if (i & ) printf(" %.1lf", *(--it));
  23. else {
  24. multiset <double> ::iterator it1 = it;
  25. printf(" %.1lf", (*it + *(--it1)) / 2.0);
  26. }
  27. }
  28. else {
  29. if (i & ) printf(" %.1lf", *it);
  30. else {
  31. ++it;
  32. multiset <double> ::iterator it1 = it;
  33. printf(" %.1lf", (*it + *(--it1)) / 2.0);
  34.  
  35. }
  36. }
  37. }
  38. printf("\r\n");
  39. }
  40.  
  41. int main(){
  42. while (~scanf("%d", &n))
  43. mid_num();
  44. return ;
  45. }

堆排序的代码也贴出来吧(原谅我太懒没有优化这代码~~~)

  1. #include <stdio.h>
  2. #include <string.h>
  3. int lstack[], ltot, rstack[], rtot, mid;
  4. int Max(int a, int b){
  5. return a > b ? a : b;
  6. }
  7. int Min(int a, int b){
  8. return a<b ? a : b;
  9. }
  10. void lup(int step){
  11. while (step != ){
  12. if (lstack[step]>lstack[step / ])lstack[step] ^= lstack[step / ] ^= lstack[step] ^= lstack[step / ];
  13. else break;
  14. step = step / ;
  15. }
  16. if (step == && lstack[] > mid) lstack[] ^= mid ^= lstack[] ^= mid;
  17. }
  18. void ldown(){
  19. if (ltot > && mid < lstack[]) mid ^= lstack[] ^= mid ^= lstack[];
  20. else return;
  21. int step = ;
  22. while (step * < ltot){
  23. if (step * + >= ltot){
  24. if (lstack[step] < lstack[step * ])
  25. lstack[step] ^= lstack[step * ] ^= lstack[step] ^= lstack[step * ], step = step * ;
  26. else return;
  27. }
  28. else{
  29. if (lstack[step] <= lstack[step * ] && lstack[step * + ] <= lstack[step * ])
  30. lstack[step] ^= lstack[step * ] ^= lstack[step] ^= lstack[step * ], step = step * ;
  31. else if (lstack[step] <= lstack[step * + ] && lstack[step * ] <= lstack[step * + ])
  32. lstack[step] ^= lstack[step * + ] ^= lstack[step] ^= lstack[step * + ], step = step * + ;
  33. else return;
  34. }
  35. }
  36. }
  37. void rup(int step){
  38. while (step != ){
  39. if (rstack[step]<rstack[step / ])rstack[step] ^= rstack[step / ] ^= rstack[step] ^= rstack[step / ];
  40. else break;
  41. step = step / ;
  42. }
  43. if (step == && rstack[]<mid) rstack[] ^= mid ^= rstack[] ^= mid;
  44. }
  45. void rdown(){
  46. if (rtot> && mid>rstack[]) mid ^= rstack[] ^= mid ^= rstack[];
  47. else return;
  48. int step = ;
  49. while (step * < rtot){
  50. if (step * + >= rtot){
  51. if (rstack[step] > rstack[step * ])
  52. rstack[step] ^= rstack[step * ] ^= rstack[step] ^= rstack[step * ], step = step * ;
  53. else return;
  54. }
  55. else{
  56. if (rstack[step] >= rstack[step * ] && rstack[step * + ] >= rstack[step * ])
  57. rstack[step] ^= rstack[step * ] ^= rstack[step] ^= rstack[step * ], step = step * ;
  58. else if (rstack[step] >= rstack[step * + ] && rstack[step * ] >= rstack[step * + ])
  59. rstack[step] ^= rstack[step * + ] ^= rstack[step] ^= rstack[step * + ], step = step * + ;
  60. else return;
  61. }
  62. }
  63. }
  64. int main()
  65. {
  66. int t = , n, i;
  67. while (scanf("%d", &n) != EOF){
  68. printf("Case %d:\r\n", t++);
  69. scanf("%d", &mid);
  70. ltot = rtot = ;
  71. printf("%.1lf", (double)mid);
  72. for (i = ; i < n; i++){
  73. if (i % ){
  74. scanf("%d", &lstack[ltot]);
  75. lup(ltot);
  76. rdown();
  77. ltot++;
  78. printf(" %.1lf", (mid + lstack[]) / 2.0);
  79. }
  80. else{
  81. scanf("%d", &rstack[rtot]);
  82. rup(rtot);
  83. ldown();
  84. rtot++;
  85. printf(" %.1lf", (double)mid);
  86. }
  87. }
  88. printf("\r\n");
  89. }
  90. return ;
  91. }

[Swust OJ 1094]--中位数(巧用set,堆排序)的更多相关文章

  1. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535   Des ...

  2. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

  3. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  4. [Swust OJ 746]--点在线上(线段树解法及巧解)

    题目链接:http://acm.swust.edu.cn/problem/746/ Time limit(ms): 1000 Memory limit(kb): 65535   fate是一个数学大牛 ...

  5. [Swust OJ 1026]--Egg pain's hzf

      题目链接:http://acm.swust.edu.cn/problem/1026/     Time limit(ms): 3000 Memory limit(kb): 65535   hzf ...

  6. [swustoj 1094] 中位数

    中位数(1094) 问题描述 中位数(又称中值,英语:Median),统计学中的专有名词,代表一个样本.种群或概率分布中的一个数值,其可将数值集合划分为相等的上下两部分.对于有限的数集,可以通过把所有 ...

  7. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...

  8. [Swust OJ 1023]--Escape(带点其他状态的BFS)

    解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535     Descript ...

  9. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

随机推荐

  1. JavaEE学习之类加载器

    类装载子系统 在JAVA虚拟机中,负责查找并装载类型的那部分被称为类装载子系统. JAVA虚拟机有两种类装载器:启动类装载器和用户自定义类装载器.前者是JAVA虚拟机实现的一部分,后者则是Java程序 ...

  2. 5.7.1.2 eval() 方法

    现在我们介绍最后一个方法,这大概是ECMAScript语言中最强大的一个方法:eval().eval()方法就想一个完整的ECMAScript解析器,它只接受一个参数,即要执行的ECMAScript( ...

  3. yii教程

    http://www.yiichina.com/doc 官网是很好的参考文档

  4. https tomcat 证书搭建

    首先生成证书说明 keytool -genkey -alias castest -keyalg RSA -keystore c:/keys/caskey 先让输入密码,密码必须记住,下面会用到 其中“ ...

  5. Oracle10g任务调度创建步骤

    /* 创建可执行程序 */begin DBMS_SCHEDULER.CREATE_PROGRAM( program_name => 'peace_sj_his.PROG_DATASYNC', p ...

  6. C#使用Windows API 隐藏/显示 任务栏 (FindWindowEx, ShowWindow)

    原文 C#使用Windows API 隐藏/显示 任务栏 (FindWindowEx, ShowWindow) 今天,有网友询问,如何显示和隐藏任务栏? 我这里,发布一下使用Windows API 显 ...

  7. Android开源项目(一)

    Android开源项目(一) GitHub在中国的火爆程度无需多~~,越来越多的开源项目迁移到GitHub平台上.更何况,基于不要重复造轮子的原则~~~~了解当下比较流行的Android与iOS开源项 ...

  8. hdoj 1532 Drainage Ditches(最大网络流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路分析:问题为最大网络流问题,给定一个有向图,需要求解该有向图的最大网络流,使用Edmonds ...

  9. 谁有SMI-S Provider的一些源码,能参考一下吗

    我要用OpenPegasus根据SMI-S规范来写provider,不知道如何下手啊,求高手指点

  10. JSP 9 大内置对象详解

    内置对象特点: 1.            由JSP规范提供,不用编写者实例化. 2.            通过Web容器实现和管理 3.            所有JSP页面均可使用 4.     ...