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. [Linux]使用宝塔面板做负载均衡时遇到的问题和解决办法

    最近公司的小程序因为高峰期访问缓慢的问题,打算用负载均衡试试.本人是个新手,在网上找了几篇负载均衡的文章看了看,最后还是用了宝塔面板的负载均衡插件...这个服务器我也是刚刚接手,很多东西都是以前的同事 ...

  2. VirtualBox + CentOS详细安装教程

    一.前期工作准备 电脑虚拟化开启(必要工作)大致流程: a.电脑开机时长按F12(F10)进入BIOS界面; b.依次选择Configuratio > Intel Virtual Technol ...

  3. jq写无缝轮播

    今天分享一下我自己早几天写的一个效果:无缝轮播,虽然不难,很简单,也没有封装处理过,但是还是希望能帮到一些前端的小伙伴吧,如果有小伙伴感觉有更简化的写法希望可以一起交流一下,技术在于交流嘛,我的邮箱是 ...

  4. restframework序列化字段和字段构造方式

  5. C# 系统应用之鼠标模拟技术及自动操作鼠标

    游戏程序的操作不外乎两种——键盘输入控制和鼠标输入控制,几乎所有游戏中都使用鼠标来改变角色的位置和方向,本文主要是讲述如何使用C#调用Windows API函数实现鼠标模拟操作的功能.首先通过结合Fi ...

  6. php 获取当前完整url地址

    echo $url = $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].$_SERVER[&q ...

  7. 学习Emmet

    学习Emmet 在进行Web开发时,就免不了需要编写html文件,如何快速(偷懒^-^)的进行编写html,是个大问题,上网找了一下,发现了一个好东西 ,视频教程可以看下小马技术Emmet爆速开发, ...

  8. dotnet Core 2.0学习笔记(一)

    一:Dotnet Core Windows运行环境,标红部分要注意 https://docs.microsoft.com/en-us/dotnet/core/windows-prerequisites ...

  9. U盘装系统流程_U启动

    步骤 (已有装机U盘的前提下,没有则参考U启动制作U盘启动盘详细教程) (这里默认已设置了相关开机设置,若没设置则参考怎么用u盘装win7系统(推荐看)中步骤11-13) 按开机键后,不断按F12(有 ...

  10. centos7.5yum安装mysql(官方yum源比较慢)

    mysql的部署 查看Linux发行版本 cat /etc/redhat-release 下载MySQL官方的Yum Repository wget -i http://dev.mysql.com/g ...