中位数(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. Vb.Net Xml文档格式化

    最近在处理Webservice文档的时候,因为是未格式化的,需要处理,所以有了以下代码. #Region "Xml字符串转换成格式化的XML文件" 'txt_Result.Text ...

  2. Demo学习: CustomException

    CustomException 捕获程序发生的异常. 1. 抛出各种异常 procedure TMainForm.UniButton1Click(Sender: TObject); begin PBy ...

  3. WPF xmal绑定数据,当显示数据过长用省略号代替的方法

    有时候会遇到这种情况,用了数据绑定显示的数据太长时,如何让过长的数据显示规定的长度,多余的用省略号代替呢,自己写了个简单的小例子和大家分享一下^_^,我也是学习WPF不久,这是我第一次写博客,有问题还 ...

  4. matlab之点运算基本思想及几何平移变换

    1.对数变换可以增强图像中较暗部分的细节,因为对数可以将较小的值放大,而较大的值缩小 2.伽马变换:y = (x + esp) ^ γ,x,y的取值范围是0到1,esp是补偿系数,γ为伽马系数.γ的不 ...

  5. Linux安装oracle 10g常见问题之——ORA-01078,LRM-00109,ORA-01102

    [oracle@toughhou database]$ sqlplus /nolog SQL> conn / as sysdba SQL> startup ORA-01078: failu ...

  6. ORA-27102: out of memory并伴随OSD-00031的处理

    刚才客户电话过来说有个数据库起不来了,开发商搞了好久搞不掂,得要让我们去帮忙看看.过去到现场,发现数据库无法打开,连nomount模式都不可以.报错的内容大致如下: ORA-27102: out of ...

  7. 【BZOJ 1834】 [ZJOI2010]network 网络扩容

    Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. 将1到N的最大流增加K所需的 ...

  8. 回顾Ado.Net

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...

  9. Python系统调用——运行其他程序

    转载:http://blog.csdn.net/ssihc0/article/details/7738527 在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其 ...

  10. 分享SCI写作经验和一些工具