中位数(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. mysql之创建外键报150错误的处理方法

    这几天由于在赶项目进度,也就没有及时记录下自己的学习情况 ,在完成项目的这段时间里,碰到了很多问题,在解决问题的过程中学习了不少技巧. 这里就主要介绍一下在mysql数据库中为表之间建立外键时报100 ...

  2. Catalyst揭秘 Day3 sqlParser解析

    Catalyst揭秘 Day3 sqlParser解析 今天我们会进入catalyst引擎的第一个模块sqlparser,它是catalyst的前置模块. 树形结构 从昨天的介绍我们可以看到sqlPa ...

  3. Python的文件与文件夹操作

    Python的文件与文件夹操作 Python OS模块 1.重命名:os.rename(old, new) 2.删除:os.remove(file) 3.列出目录下的文件 :os.listdir(pa ...

  4. CentOS 6.5 安装与配置LAMP

    准备工作: 1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --d ...

  5. SecureCRT上传bash: rz: command not found(转载)

    转载自:http://blog.csdn.net/zhangdaiscott/article/details/18141017 -bash: rz: command not found rz命令没找到 ...

  6. C# DateTime和DateTime?格式化时间

    DateTime: <%= Model.CreateTime.ToString("yyyy年MM月dd日 H时m分s秒")%>   DateTime?: <%= ...

  7. centos7安装mplayer以及出现的各种问题

    首先,centos7默认的视频播放器基本不能用,这里我们选择mplayer作为视频播放器. 安装的过程,痛并快乐着....... 首先我们去mplayer的官网下载需要的文件,http://www.m ...

  8. spoj 39

    DP  完全背包问题 的   d[i] = min(d[i], d[i-w]+p)   d[i]表示当总重量为i时可装的最小价值 #include <cstdio> #include &l ...

  9. Akka Stream文档翻译:Motivation

    动机 Motivation The way we consume services from the internet today includes many instances of streami ...

  10. 一个比较全面的DJANGO_REST_FRAMEWORK的CASE

    验证啊,过滤啊,hypermedia as the engine of ap‐plication state (HATEOAS)啊都有的. urls.py __author__ = 'sahara' ...