[swustoj 1094] 中位数
中位数(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] 中位数的更多相关文章
- [Swust OJ 1094]--中位数(巧用set,堆排序)
题目链接:http://acm.swust.edu.cn/problem/1094/ Time limit(ms): 1000 Memory limit(kb): 32768 中位数(又称中值,英 ...
- [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 ...
- [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 ...
- BZOJ1303 [CQOI2009]中位数图
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 在MySQL中,如何计算一组数据的中位数?
要得到一组数据的中位数(例如某个地区或某家公司的收入中位数),我们首先要将这一任务细分为3个小任务: 将数据排序,并给每一行数据给出其在所有数据中的排名. 找出中位数的排名数字. 找出中间排名对应的值 ...
- AC日记——中位数 洛谷 P1168
题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], …, A[2k - 1]的中位数.[color=red]即[/color] ...
- [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)
题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...
- LeetCode 4 Median of Two Sorted Arrays 查找中位数,排除法,问题拓展 难度:1
思路:设现在可用区间在nums1是[s1,t1),nums2:[s2,t2) 1.当一个数组可用区间为0的时候,由于另一个数组是已经排过序的,所以直接可得 当要取的是最小值或最大值时,也直接可得 2. ...
- BZOJ 1303 CQOI2009 中位数图 水题
1303: [CQOI2009]中位数图 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2340 Solved: 1464[Submit][Statu ...
随机推荐
- PyQt 5.2 发布,此版本完全支持Qtv5.2.0
PyQt 5.2 发布,此版本完全支持Qtv5.2.0 :包括了新的 QtBluetooth,QtPositioning,QtMacExtras,QtWinExtras 和 Qt11Extras 模板 ...
- PhantomJS实现最简单的模拟登录方案
以前写爬虫,遇到需要登录的页面,一般都是通过chrome的检查元素,查看登录需要的参数和加密方法,如果网站的加密非常复杂,例如登录qq的,就会很蛋疼 在后面,有了Pyv8,就可以把加密的js文件扔给它 ...
- WPF简单的口算案例
前几天在博客园,看到有博友利用Winform做了一个口算案例,于是我想把它移植在WPF程序中.Winform程序:http://www.cnblogs.com/ImYZF/p/3345452.html ...
- PCB优化设计(二) 转载
PCB优化设计(二) 2011-04-25 11:41:05| 分类: PCB设计 目 前SMT技术已经非常成熟,并在电子产品上广泛应用,因此,电子产品设计师有必要了解SMT技术的常识和可制造性 ...
- IOS UIVIEW layer动画 总结(转)
转发自:http://www.aichengxu.com/article/%CF%B5%CD%B3%D3%C5%BB%AF/16306_12.html IOS UIVIEW layer动画 总结, ...
- 【数位DP】Hdu 3652:B-number
B-number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- centos64位安装32位C/c++库
yum install glibc.i686 glibc-devel.i686 yum install libstdc++.i686yum install libstdc++-devel.i686yu ...
- KafkaSpout的处理流程
基于0.93版本Storm 首先,如果自己写KafkaSpout,该怎么办?有哪些地方需要考虑呢 1. 得实现Storm指定的接口.这样Storm才能够使用它.那么需要实现什么接口?需要提供什么功能给 ...
- Ubuntu环境下手动配置Hadoop1.2.1
/×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...
- POJ1228+凸包
见代码. /* 凸包(稳定凸包) 题意:给出一些点,这些点要么是凸包的顶点要么是边上的. 证明每条边上都至少有3个点. */ #include<stdio.h> #include<s ...