For n elements x1, x2, ..., xn with positive integer weights w1, w2, ..., wn. The weighted median is the element xk satisfying 
 and   , S indicates  

Can you compute the weighted median in O(n) worst-case?
 

Input

There are several test cases. For each case, the first line contains one integer n(1 ≤  n ≤ 10^7) — the number of elements in the sequence. The following line contains n integer numbers xi (0 ≤ xi ≤ 10^9). The last line contains n integer numbers wi (0 < wi < 10^9).
 

Output

One line for each case, print a single integer number— the weighted median of the sequence.
 

Sample Input

7
10 35 5 10 15 5 20
10 35 5 10 15 5 20

Sample Output

20

Hint

The S which indicates the sum of all weights may be exceed a 32-bit integer. If S is 5,  equals 2.5.
 
这是一道山东省省赛的题,英语不好的我看了以后一脸懵逼,最后花了好长时间才看懂,原来很是简单

题目意思:
给一些数x和它们对应的权值w,按照如图所示公式,s是所有权值w的总和。
求一个xk,使得满足前两个公式。

解题思路:
用结构体保存元素值和权值,先升序排列,累计xk之前的权值和,直到该权值大于s。

#include<stdio.h>
#include<algorithm>
using namespace std;
struct message{
int x;
int w;
}a[10000010];
int my_cmp(message a,message b)
{
if(a.x<b.x)
return 1;
else
return 0;
}
int main()
{
int n,i;
double s,y;
while(scanf("%d",&n)!=EOF)
{
s=0;
for(i=0;i<n;i++)
scanf("%d",&a[i].x);
for(i=0;i<n;i++)
{
scanf("%d",&a[i].w);
s=s+a[i].w;
}
s=s/2.0;
y=0;
sort(a,a+n,my_cmp);
for(i=0;i<n;i++)
{
y=y+a[i].w;
if(y>s)
{
printf("%d\n",a[i].x);
break;
}
}
}
return 0;
}

  

Weighted Median的更多相关文章

  1. CVPR论文《100+ Times Faster Weighted Median Filter (WMF)》的实现和解析(附源代码)。

    四年前第一次看到<100+ Times FasterWeighted Median Filter (WMF)>一文时,因为他附带了源代码,而且还是CVPR论文,因此,当时也对代码进行了一定 ...

  2. 第五届山东ACM大赛汇总

    A.angry_birds_again_and_again 简单积分: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem& ...

  3. {ICIP2014}{收录论文列表}

    This article come from HEREARS-L1: Learning Tuesday 10:30–12:30; Oral Session; Room: Leonard de Vinc ...

  4. 山东省第五届ACM省赛

    题目链接:http://acm.sdut.edu.cn/sdutoj/contest_show.php?contest_id=1449 相关总结:http://www.cnblogs.com/mcfl ...

  5. AdaBoostRegressor

    class sklearn.ensemble.AdaBoostRegressor(base_estimator=None, n_estimators=50, learning_rate=1.0, lo ...

  6. ONCOCNV软件思路分析之tumor处理

    前期处理 perl脚本统计RC(RC(read counts)) 读入control baseline 和 sigma(最后baseline 预测的mad值) 将gc < 0.28或gc > ...

  7. Image Filter

    香港中文大学研究成果 Rolling Guidance Filter http://www.cse.cuhk.edu.hk/~leojia/projects/rollguidance/ 100+ Ti ...

  8. PatchMatch小详解

    最近发了两片patch match的,其实自己也是有一些一知半解的,找了一篇不知道谁的大论文看了看,又回顾了一下,下面贴我的笔记. The PatchMatch Algorithm patchmatc ...

  9. Robust Locally Weighted Regression 鲁棒局部加权回归 -R实现

    鲁棒局部加权回归 [转载时请注明来源]:http://www.cnblogs.com/runner-ljt/ Ljt 作为一个初学者,水平有限,欢迎交流指正. 算法参考文献: (1) Robust L ...

随机推荐

  1. ABAP术语-URL

    URL 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/20/1114193.html Uniform Resource Locator (U ...

  2. 【postgresql的使用】

    #安装: #初始化: #允许远程登录: #创建数据库并指定用户 #创建用户 #列出数据库 #进入数据库 #查询数据 #or(或)查询 #and ,or(和,或查询) #表连接 #内,外(左右),交叉连 ...

  3. Python学习笔记十一:模块

    标准库(内置模块) time与datetime 模块 时间表示方式 1.时间戳 2.格式化的字符串 3.元组形式 时间戳就是一个秒数 x=time.time(),从1970年1月1日0时0分0秒到现在 ...

  4. ESP32 学习笔记 - Ubuntu安装

    最近买了一个ESP32的开发板,以前只玩过ESP8266,没经验只能自己一步步摸索 现在把学习的过程记录下来,以免过了一段时间就忘记了 第一步安装 VMware 可以在我的网盘下载 链接:https: ...

  5. GO富集分析柱状图

    target_gene_id <- unique(read.delim("miRNA-gene interactions.txt")$EntrezID) # BiocInst ...

  6. POJ 3683 Priest John's Busiest Day 【2-Sat】

    这是一道裸的2-Sat,只要考虑矛盾条件的判断就好了. 矛盾判断: 对于婚礼现场 x 和 y,x 的第一段可以和 y 的第一段或者第二段矛盾,同理,x 的第二段可以和 y 的第一段或者第二段矛盾,条件 ...

  7. Lingo安装

    Lingo安装 Lingo简介        LINGO是Linear Interactive and General Optimizer的缩写,即"交互式的线性和通用优化求解器" ...

  8. Using a custom AxisRenderer object

    Using a custom AxisRenderer object http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d1 ...

  9. 20155217 2016-2017-2《Java程序设计》课程总结

    20155217 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业一:我所期望的师生关系 预备作业二:c与java的关系 预备作业三:安装虚拟机 第一周作业: ...

  10. 20155336 实验三 敏捷开发与XP实践

    20155336 实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验内容及步骤 (一)编码标准:在IDEA中使用工具(Code->Reformate Code)把代码重 ...