山东省第五届ACM省赛题——Weighted Median(模拟)
来源:互联网 发布:淘宝话费充值利润 编辑:程序博客网 时间:2024/05/22 17:35
题目描述
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?
输入
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).
输出
One line for each case, print a single integer number— the weighted median of the sequence.
示例输入
7
10 35 5 10 15 5 20
10 35 5 10 15 5 20
示例输出
20
本来看到n是10^7就想着是不是有什么高效的算法,结果一看别人做的才知道可以直接模拟过程,从1到n循环,结果700多MS过了。。。
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#include <bitset>#define MAXN 10000010using namespace std;struct Number{ int x,w;};Number num[MAXN];bool cmp(Number a,Number b){ return a.x<b.x;}long long sum,first,last,ans;int main(){ double S; int n,m,i; while(~scanf("%d",&n)) { sum=0; for(i=1; i<=n; ++i) scanf("%d",&num[i].x); for(i=1; i<=n; ++i) { scanf("%d",&num[i].w); sum+=num[i].w; } sort(num+1,num+1+n,cmp); S=sum*0.5; first=last=0; for(i=1;i<=n-1;++i) { first+=num[i].w; last=sum-first-num[i+1].w; if(first<S&&last<=S) { ans=num[i+1].x; break; } } printf("%lld\n",ans); } return 0;}
0 0
- 山东省第五届ACM省赛题——Weighted Median(模拟)
- 第五届山东省ACM Weighted Median
- 山东省第五届ACM大学生程序设计竞赛-Weighted Median(模拟)
- Weighted Median(山东省第五届ACM大学生程序设计竞赛 )
- 2014山东省第五届ACM省赛 Weighted Median
- 2014年山东省第五届ACM--Weighted Median
- 山东省第五届ACM大学生程序设计竞赛 Weighted Median
- 第五届山东省赛J--Weighted Median
- 山东省第三届ACM省赛题——The Best Seat in ACM Contest(模拟)
- 山东省第五届ACM省赛题——Full Binary Tree(二叉树应用)
- 山东省第五届ACM省赛题——angry_birds_again_and_again(计算几何)
- 山东省第五届ACM省赛题——Hearthstone II(第二类Stiring数)
- 山东省第五届ACM省赛题——Colorful Cupcakes(四维dp)
- 山东省第五届ACM省赛题——Circle(递推求概率)
- 山东省第五届ACM省赛题——Painting Cottages(求点集的划分)
- 山东省第五届ACM省赛题——Devour Magic(线段树区间覆盖+更新)
- 山东省第四届ACM省赛题——Contest Print Server(模拟)
- 第五届山东省ACM Circle(高斯消元)
- linux bash shell中,单引号、 双引号,反引号(``)的区别及各种括号的区别
- 使用苹果地图定位地理信息反编码 遇到 Error Domain=kCLErrorDomain Code=8 问题
- Android Animation
- Am命令用法
- IOS学习之——UItouch的相关使用
- 山东省第五届ACM省赛题——Weighted Median(模拟)
- Android初学习 - Service 中的 onStart 和 onStartCommand
- unity 实现流光效果
- Linux中变量#,#,@,0,0,1,2,2,*,$$,$?的含义
- dpi 、 dip 、分辨率、屏幕尺寸、px、density 关系以及换算
- FLash Ram 的区别
- MUI中的accordion(折叠面板)
- NAT穿透的详解及分析(NAT类型举例解释及穿透技术)
- Pm命令用法