中位数(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. Beaglebone Back学习三(开发环境搭建)

    开发环境搭建 1 Ubuntu环境搭建 2 Window环境搭建 3 开发板环境搭建 1 Ubuntu环境搭建 (1)安装必要的网络工具 samba nfs tftp vmware-tools sam ...

  2. 简易记事本(演示java文件io)

      演示效果:  打开txt文件 输入文字,保存 选择保存地址 生成文件 源代码: package io; import java.io.*; import java.awt.*; import ja ...

  3. Kinetic使用注意点--collection

    new Collection() 扩展了数组,主要用于配合new Container().get()使用 方法: each(func):遍历数组,执行回调函数.回调函数接收两个值,节点和索引. toA ...

  4. 编辑器&IDE中适合程序员的字体

    adobe的免费字体 source Code Pro

  5. Tesseract初探

    一.框架介绍 Tesseract 是一款图片识别工具,可以抓取图片中的文字,可以支持多种语言(默认是英语),需要下载开源文件可以在github上下载,如果知识应用不想太多深究直接在google cod ...

  6. Java中的IO流系统详解

    Java 流在处理上分为字符流和字节流.字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符.字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组. Java 内用 U ...

  7. 【HDU2222】Keywords Search

    Problem DescriptionIn the modern time, Search engine came into the life of everybody like Google, Ba ...

  8. mvc权限,登陆,异常

    public class FilterOfPer : ActionFilterAttribute     {         public override void OnActionExecutin ...

  9. ios实现截屏(转)

    -(UIImage*) makeImage {  UIGraphicsBeginImageContext(self.view.bounds.size);  [self.view.layer rende ...

  10. linux编程之指针

    这个是数组指针.指针数组.二维数组之间相互转换的代码 #include<stdio.h> void main() { ][]={,,,,,,,}; int *b=NULL; int **c ...