sdut 2886 Weighted Median 结构体
来源:互联网 发布:日本校园霸凌 知乎 编辑:程序博客网 时间:2024/06/02 03:00
Weighted MedianTime Limit: 2000MS Memory limit: 65536K
题目描述
For n elements x1, x2, ..., xn with positive integer weights w1, w2, ..., wn. The weighted median is the element xk satisfying
and , S indicates
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.
示例输入
710 35 5 10 15 5 2010 35 5 10 15 5 20
示例输出
20
提示
The S which indicates the sum of all weights may be exceed a 32-bit integer. If S is 5, equals 2.5.
#include <iostream>#include<cstdio>#include<string.h>#include<algorithm>using namespace std;typedef long long LL;const int Max=1e7+2;int ans,n;struct Node{ int x,w;}node[Max];int cmp(Node a,Node b){ return a.x<b.x;}int main(){ while(~scanf("%d",&n)) { long long sum=0; for(int i=0; i<n; i++) scanf("%d",&node[i].x); for(int i=0; i<n; i++) { scanf("%d",&node[i].w); sum+=node[i].w; } long long s=sum/2.0; long long minn=0,maxx=0; sort(node,node+n,cmp); for(int i=0;i<n;i++) { minn+=node[i-1].w; maxx=sum-minn-node[i].w; if(minn<s&&maxx<=s) { ans=node[i].x; break; } } cout<<ans<<endl; } return 0;}
0 0
- sdut 2886 Weighted Median 结构体
- SDUT 2886 - Weighted Median (思维)
- [ACM] SDUT 2886 Weighted Median (省赛最悲剧的一道题)
- 第五届山东省赛J--Weighted Median
- 第五届山东省ACM Weighted Median
- 2014山东省第五届ACM省赛 Weighted Median
- 2014年山东省第五届ACM--Weighted Median
- 山东省第五届ACM大学生程序设计竞赛 Weighted Median
- Weighted Median(山东省第五届ACM大学生程序设计竞赛 )
- sdut 2409 dp 打表 结构体
- 山东省第五届ACM省赛题——Weighted Median(模拟)
- 山东省第五届ACM大学生程序设计竞赛-Weighted Median(模拟)
- sdut Find a girl friend(结构体的二级排序)
- sdut--Find a girl friend(结构体的二级排序)
- Weighted average
- 数组结构体实现稀疏矩阵转置 (sdut oj 3347)
- SDUT 1117 求绝对值(选择结构)
- SDUT 1122 判断素数(循环结构)
- SQL Server 存储过程
- 如何发布一个Python命令行工具
- pregel 与 spark graphX 的 pregel api
- 主外键关联取出主键时报错[org.hibernate.LazyInitializationException] could not initialize proxy - no Session
- FFmpeg:'UINT64_C' was not declared in ths scope
- sdut 2886 Weighted Median 结构体
- Spark组件之SparkR学习3--使用spark-submit向集群提交R代码文件data-manipulation.R
- Simple, Piso, Icofoam等不可压求解器
- 利用session做国际化引起的old区内存爆满及修复方法
- verilog过程块与赋值
- ABAP 动态生成内表的几种方法
- ZOJ 3876--May Day Holiday
- 《sort命令的k选项大讨论》-linux命令五分钟系列之二十七
- 趣味整数-黑洞数