POJ_3579_Median_(二分,查找第k大的值)
描述
http://poj.org/problem?id=3579
给你一串数,共C(n,2)个差值(绝对值),求差值从大到小排序的中值,偶数向下取.
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5468 | Accepted: 1762 |
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
Source
分析
可以先把数排序,然后下界0,上界a[n]-a[1],二分假定中值d,如果所有差值中大于等于d的小于等于N/2,说明d太大了.判断d是否可行时如果枚举差值就太慢了,可以对于每一个数x,找所有满足xi>=x+d(xi>x)的xi的个数,这里还是用二分,直接lower_bound即可.
注意:
1.差值共有N=C(n,2)=n*(n-1)/2而不是n.
2.数据范围并不会超int.
#include<cstdio>
#include<algorithm>
using std :: sort;
using std :: lower_bound; const int maxn=;
int n,N;
int a[maxn]; bool C(int d)
{
int cnt=;
for(int i=;i<n;i++) cnt+=a+n-(lower_bound(a+i+,a+n+,a[i]+d)-);
return cnt<=N/;
} void solve()
{
sort(a+,a+n+);
int l=,r=a[n]-a[];
while(l<r)
{
int m=l+(r-l+)/;
if(C(m)) r=m-;
else l=m;
}
printf("%d\n",l);
} void init()
{
while(scanf("%llu",&n)==)
{
N=n*(n-)/;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
solve();
}
} int main()
{
init();
return ;
}
POJ_3579_Median_(二分,查找第k大的值)的更多相关文章
- POJ_3685_Matrix_(二分,查找第k大的值)
描述 http://poj.org/problem?id=3685 一个n*n的矩阵,(i,j)的值为i*i+100000*i+j*j-100000*j+i*j,求第m小的值. Matrix Time ...
- POJ 3579 3685(二分-查找第k大的值)
POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...
- 查找第K大的值
这种题一般是给定N个数,然后N个数之间通过某种计算得到了新的数列,求这新的数列的第K大的值 POJ3579 题意: 用$N$个数的序列$x[i]$,生成一个新序列$b$. 新的序列定义为:对于任意的$ ...
- poj 3685 Matrix(二分搜索之查找第k大的值)
Description Given a N × N matrix A, whose element × i + j2 - × j + i × j, you are to find the M-th s ...
- poj 3579 Median (二分搜索之查找第k大的值)
Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...
- poj 2579 中位数问题 查找第K大的值
题意:对列数X计算∣Xi – Xj∣组成新数列的中位数. 思路:双重二分搜索 对x排序 如果某数大于 mid+xi 说明在mid后面,这些数的个数小于 n/2 的话说明这个中位数 mid 太大 反之太 ...
- hihoCoder 1133 二分·二分查找之k小数(TOP K算法)
#1133 : 二分·二分查找之k小数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回里我们知道Nettle在玩<艦これ>,Nettle的镇守府有很 ...
- 利用划分树求解整数区间内第K大的值
如何快速求出(在log2n的时间复杂度内)整数区间[x,y]中第k大的值(x<=k<=y)? 其实我刚开始想的是用快排来查找,但是其实这样是不行的,因为会破坏原序列,就算另外一个数组来存储 ...
- hiho week 37 P1 : 二分·二分查找之k小数
P1 : 二分·二分查找之k小数 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 在上一回里我们知道Nettle在玩&l ...
随机推荐
- Struts2 多文件下载
Step1:导入支持jar包 commons-fileupload-1.3.1.jar commons-io-2.4.jar jstl-1.2.jar standard-1.1.2.jar commo ...
- OC加强-day04
#pragma mark 00知识回顾 //定义一个函数 函数没有返回值函数有一个参数:返回值是double 参数是两个int的block void test(int a); void test(do ...
- UiTextField对输入的长度进行限制并提示用户还可输入的长度
最近想做用户昵称的限制,但是网上百度了很多方法效果都不是我自己想要的,终于找到种方法 如下: 1.声明两个属性 nickname是昵称的textfleld canEditSizeLAbel是提示用户剩 ...
- js hashMap
/** * MAP对象,实现MAP功能 * * 接口: * size() 获取MAP元素个数 * isEmpty() 判断MAP是否为空 * clear() 删除MAP所有元素 * put(key, ...
- Eclipse使用Maven创建Web项目
一.Maven插件下载.jdk下载 1.maven下载地址: http://maven.apache.org/download.cgi 2.jdk下载地址: http://www.oracle.com ...
- 【制作镜像Win*】文件准备
mkdir /var/image-createcd /var/image-create/ 在物理机上: wget http://10.254.3.75/images/libvirt/libvirt.x ...
- (hdu)1285 确定比赛名次
Problem Description 有N个比赛队(<=N<=),编号依次为1,,,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接 ...
- Linux Master/Baremetal Remote 配置下的裸机调试
为了实现在ZC702开发板上的两颗Cortex-A9处理器上实现Linux Master/Baremetal Remote 配置,并对Remote端的裸机程序进行调试,需要注意的几点如下: 一.建立p ...
- 检测 IE 版本 in Javascript
点击打开链接http://stackoverflow.com/questions/10964966/detect-ie-version-in-javascript <!doctype html& ...
- hadoop1 和haddop2 mapperreducer的wordcount详解
转 mapreduce中wordcount详细介绍(包括hadoop1和hadoop2版本) 发表于1年前(2014-04-24 10:08) 阅读(1458) | 评论(0) 1人收藏此文章, ...