POJ 3579 median 二分搜索,中位数 难度:3
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3866 | Accepted: 1130 |
Description
Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numbers: ∣Xi - Xj∣ (1 ≤ i < j ≤ N). We can get C(N,2) differences through this work, and now your task is to find the median of the differences as quickly as you can!
Note in this problem, the median is defined as the (m/2)-th smallest number if m,the amount of the differences, is even. For example, you have to find the third smallest one in the case of m = 6.
Input
The input consists of several test cases.
In each test case, N will be given in the first line. Then N numbers are given, representing X1, X2, ... , XN, ( Xi ≤ 1,000,000,000 3 ≤ N ≤ 1,00,000 )
Output
For each test case, output the median in a separate line.
Sample Input
4
1 3 2 4
3
1 10 2
Sample Output
1
8
思路: 一眼可知把原数列a排列一遍的时间还是有的,但是要想得到两两之差然后归并则T,M都超,计数范围也太大了,
把a排序之后两两相减,得到相邻差数组b,那么b就可以构成所有需要的差了,
那么现在假设有个差delta,使得a[i]+delta<=a[j],a[i]+delta>a[j-1],那么delta一定大于a[j-1]-a[i],a[j-2]-a[i]....delta一定小于等于a[j]-a[i],a[j+1]-a[i],
使用low_bound可以在logn时间内求出j,nlogn时间就可以求出所有比delta大的差的个数,
这时就可以判断是否delta算是差的中位数.
那么直接二分差即可,总时间n*logn*logn,5*1e7-1e8的数量级,可以跑过
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
long long halfh,h;
int n;
bool judge(int mid){
long long cnt=0;
for(int i=0;i<n;i++){
cnt+=a+n-lower_bound(a+i,a+n,a[i]+mid);
}
return cnt>halfh;
}
int main(){
while(scanf("%d",&n)==1){
h=n*(n-1)/2;halfh=h/2;
for(int i=0;i<n;i++){scanf("%d",a+i);}
sort(a,a+ n);
int l=0,r=a[n-1],mid;
while(r-l>1){
mid=r+l>>1;
if(judge(mid)){
l=mid;
}
else r=mid;
}
printf("%d\n",l);
}
}
POJ 3579 median 二分搜索,中位数 难度:3的更多相关文章
- poj 3579 Median (二分搜索之查找第k大的值)
Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...
- POJ 3579 Median(二分答案+Two pointers)
[题目链接] http://poj.org/problem?id=3579 [题目大意] 给出一个数列,求两两差值绝对值的中位数. [题解] 因为如果直接计算中位数的话,数量过于庞大,难以有效计算, ...
- POJ 3579 Median(二分答案)
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11599 Accepted: 4112 Description G ...
- POJ 3579 Median 二分加判断
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12453 Accepted: 4357 Descripti ...
- POJ 3579 Median (二分)
...
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- POJ 3579 Median 【二分答案】
<题目链接> 题目大意: 给出 N个数,对于存有每两个数的差值的序列求中位数,如果这个序列长度为偶数个元素,就取中间偏小的作为中位数. 解题分析: 由于本题n达到了1e5,所以将这些数之间 ...
- POJ 3579 3685(二分-查找第k大的值)
POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...
- POJ 3579:Median 差值的中位数
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4680 Accepted: 1452 Descriptio ...
随机推荐
- git 指定要提交的ssh key
问题描述 ssh具有-i选项,用于告知在验证时使用哪个私钥文件: -i identity_file Selects a file from which the identity (private ke ...
- Maven profile 打包分环境加载不同的资源文件
在实际开发项目中,常常有几种环境,一般情况下最少有三种环境:开发.测试.正式. 各个环境之间的参数各不相同,比如mysql.等不同环境的host不一样,若每个环境都手动替换环境很容易出错,这里我们利用 ...
- Where is HttpContent.ReadAsAsync?
It looks like it is an extension method (in System.Net.Http.Formatting): HttpContentExtensions Class ...
- git的软件安装
1.Git for Winodws 1.*的版本 https://github.com/msysgit/msysgit/releases 2.*的版本 https://github.com/g ...
- 51nod 1428 活动安排问题 (贪心+优先队列)
来源:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 首先按照开始时间从小到大排序. 其实只要维护一个结束时间的最 ...
- mysql处理时间戳
select name,telphone,FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i') as add_time from tf_apply_join order b ...
- Xcode集成POD教程
http://www.cocoachina.com/ios/20150410/11526.html COCOAPODS的网站上有很多非常好用的资源,这里来说一下如何把POD集成到我们的Xcode项目中 ...
- [ios]received memory warning
参考:http://blog.sina.com.cn/s/blog_68661bd80101nn6p.html IPhone下每个app可用的内存是被限制的,如果一个app使用的内存超过20M,则系统 ...
- C#通过Oracle.ManagedDataAccess无法访问Oralce
问题描述:通过C#引用Oracle.ManagedDataAccess.dll访问Oracle,写了如下一段代码,在本机能正常访问,但是将编译后的exe放到服务器上面就无法访问了,一直提示登录失败.而 ...
- Java读写记事本文件
Java中我们也会考虑读写记事本,文件读取如下: public static void main(String[] args) { try { String path="d:\\abc.tx ...