中位数(1094)

问题描述

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

输入

多组输入

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

输出

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

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

样例输入

5
1 2 3 4 5
6
2 5 4 8 7 4

样例输出

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

方法1:简单线段树

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 1000010 int n;
int a[N];
int b[N];
int c[N];
int cnt[N<<]; void pushup(int rt)
{
cnt[rt]=cnt[rt<<]+cnt[rt<<|];
}
void build(int n)
{
memset(cnt,,sizeof(cnt));
}
void update(int l,int r,int rt,int pos)
{
if(l==r)
{
cnt[rt]++;
return;
}
int m=(l+r)>>;
if(pos<=m) update(l,m,rt<<,pos);
else update(m+,r,rt<<|,pos);
pushup(rt);
}
int query(int l,int r,int rt,int c)
{
if(l==r) return l;
int m=(l+r)>>;
if(c<=cnt[rt<<]) query(l,m,rt<<,c);
else return query(m+,r,rt<<|,c-cnt[rt<<]);
}
int main()
{
int iCase=;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b+n+);
for(int i=;i<=n;i++)
{
int t=a[i];
a[i]=lower_bound(b+,b+n+,a[i])-b;
c[a[i]]=t;
}
build(n);
printf("Case %d:\r\n",iCase++);
for(int i=;i<=n;i++)
{
if(i-) printf(" ");
update(,n,,a[i]);
if(i&) printf("%.1f",double(c[query(,n,,i/+)]));
else printf("%.1f",(c[query(,n,,i/)]+c[query(,n,,i/+)])/2.0);
}
printf("\r\n");
}
return ;
}

方法2:使用两个优先队列,前者存前一半的数,后者存后一半的数即可

#include<iostream>
#include<cstdio>
#include<queue>
#include<functional>
#include<algorithm>
using namespace std;
#define N 1000010 int n;
int num;
float ans[N]; priority_queue<int,vector<int>,less<int> > q1; //存放前(n+1)/2位数字,大数为优先级最大
priority_queue<int,vector<int>,greater<int> > q2; //存放前(n-1)/2位数字,小数为优先级最大 int main()
{
int iCase=;
while(scanf("%d",&n)!=EOF)
{
while(!q1.empty())q1.pop();
while(!q2.empty())q2.pop(); for(int i=;i<=n;++i)
{
scanf("%d",&num);
if(i==) q1.push(num);
else
{
if(num<=q1.top()) q1.push(num);
else q2.push(num);
}
if(q1.size()>q2.size()+)
{
q2.push(q1.top());
q1.pop();
}
if(q1.size()<q2.size())
{
q1.push(q2.top());
q2.pop();
}
if(i&) ans[i]=q1.top();
else ans[i]=(q1.top()+q2.top())/2.0;
} printf("Case %d:\r\n",iCase++);
for(int i=;i<=n;++i)
{
if(i!=) printf(" ");
printf("%.1f",ans[i]);
}
printf("\r\n");
}
return ;
}

[swustoj 1094] 中位数的更多相关文章

  1. [Swust OJ 1094]--中位数(巧用set,堆排序)

    题目链接:http://acm.swust.edu.cn/problem/1094/ Time limit(ms): 1000 Memory limit(kb): 32768   中位数(又称中值,英 ...

  2. [LeetCode] Find Median from Data Stream 找出数据流的中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  3. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  4. BZOJ1303 [CQOI2009]中位数图

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. 在MySQL中,如何计算一组数据的中位数?

    要得到一组数据的中位数(例如某个地区或某家公司的收入中位数),我们首先要将这一任务细分为3个小任务: 将数据排序,并给每一行数据给出其在所有数据中的排名. 找出中位数的排名数字. 找出中间排名对应的值 ...

  6. AC日记——中位数 洛谷 P1168

    题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], …, A[2k - 1]的中位数.[color=red]即[/color] ...

  7. [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)

    题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...

  8. LeetCode 4 Median of Two Sorted Arrays 查找中位数,排除法,问题拓展 难度:1

    思路:设现在可用区间在nums1是[s1,t1),nums2:[s2,t2) 1.当一个数组可用区间为0的时候,由于另一个数组是已经排过序的,所以直接可得 当要取的是最小值或最大值时,也直接可得 2. ...

  9. BZOJ 1303 CQOI2009 中位数图 水题

    1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2340  Solved: 1464[Submit][Statu ...

随机推荐

  1. 将Ecshop后台fckeditor升级更改为kindeditor 4.1.10编辑器

    ecshop在win8部分电脑上,不管用任何浏览器,都打不开,即使升级到最新版本都不行,问题应该吃在fckeditor兼容上.fckeditor 很久未升级,换掉该编辑器是最佳方法 第一步:下载kin ...

  2. VS2005打开VS2008 VS2010 VS2012

    我用vs2005较多,但网上找的代码经常是08 10 或者2012的,总结了以下技巧可以打开工程比较小巧的高版本代码. <1>用记事本打开解决方案文件“解决方案名.sln”,然后修改最上面 ...

  3. 怎样下载安装Firebug和使用Firebug

    Firebug是基于火狐(FireFox)浏览器的一个插件,它的作用是给Web页面开发者一个很好的测试前端页面代码的工具.所以深受网页开发者或网页布局爱好者的喜爱.像我们用DIV+CSS和html所写 ...

  4. Import和SQL*Loader这2个工具的异同

    问:请讲述Import和SQL*Loader这2个工具的异同? 解答: 相同点:这两个ORACLE工具都是用来将数据导入数据库的. 区别是: IMPORT工具只能处理由另一个ORACLE工具EXPOR ...

  5. JSP页面时间动态显示 (转载)

    <script type="text/javascript">       function startTime(){        var today=new Dat ...

  6. 【多路复用】I/O多路复用

    http://www.tuicool.com/articles/RBvqUz C#下用select方法实现socket服务端

  7. 【cheerio】nodejs的抓取页面模块

    http://baike.baidu.com/link?url=8V1CZsEzNE05ujOzISquom_pvFj16sWu1rRb8js11pmd9HNq7ePW_aKfG9oyXj6Txuu5 ...

  8. EXTJS 资料 Ext.Ajax.request 获取返回数据

    下面是一个登陆页面调用的EXTJS login function,通过 url: '/UI/HttpHandlerData/Login/Login.ashx',获取返回登陆账户和密码! Ext.onR ...

  9. css3 简单界面动画

    asdasdasdasda asdasdasdasda

  10. 'vt100': unknown terminal type.

    在Linux终端执行clear或top命令时出现:vt100: unknown terminal type的错误 1.临时办法,下次启动失效,需要重新执行 执行以下命令 $ printenv | gr ...