POJ 3579 二分
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7687 | Accepted: 2637 |
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
//两重二分,先将a数组从小到大排序,第一重二分中位数的值m,第二重枚举a[i],找到有多少
//个 |a[j]-a[i]| >= m,将这个数量与n*(n-1)/4比较作为二分的判断条件
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=;
int n,a[maxn];
ll k;
bool solve(int m){
ll cnt=;
for(int i=;i<n;i++){
int id=lower_bound(a+i,a+n,a[i]+m)-a;
cnt+=n-id;
}
return cnt>k;
}
int main()
{
while(scanf("%d",&n)==){
k=1LL*n*(n-)/;
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int l=,r=,ans;
while(l<=r){
int m=(l+r)>>;
if(solve(m)){
ans=m;
l=m+;
}else r=m-;
}
printf("%d\n",ans);
}
return ;
}
POJ 3579 二分的更多相关文章
- POJ 3579 3685(二分-查找第k大的值)
POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...
- POJ - 2018 二分+单调子段和
依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...
- 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 】Median(二分)
Median Descriptions 给N数字, X1, X2, ... , XN,我们计算每对数字之间的差值:∣Xi - Xj∣ (1 ≤ i < j ≤N). 我们能得到 C(N,2) 个 ...
随机推荐
- Shell 常用命令、基本用法总结
Filter Filter 常用于从大量文本.数据中提取需求的部分.下面介绍几个常用的 filter 命令. cut $ cut -c 5-8 textfile.txt # 切出 textfile.t ...
- Python3 下安装python-votesmart
在python2下安装python-smart还比较容易,而python3中由于很多函数库的变化直接使用python setup.py install 命令来安装的话会导致错误,而导致错误的原因就是p ...
- leetcode个人题解——#17 Letter Combinations of a Phone Number
思路:用深搜遍历九宫格字符串,一开始做的时候发生了引用指向空地址的问题,后来发现是vector不能直接=赋值. class Solution { public: int len; ]={"a ...
- java一些面试题
java虚拟机 什么时候会触发full gc System.gc()方法的调用 老年代空间不足 永生区空间不足(JVM规范中运行时数据区域中的方法区,在HotSpot虚拟机中又被习惯称为永生代或者永生 ...
- 20届的阿里 头条 网易 滴滴 百度 小米等Java面经
转载连接 个人博客:junxuelian.cn 总结:个人感觉回答面试官问题不必太官方和书面化,腾讯sng招实习被发现照着百度百科念.结果可想而知.用自己的话和理解去回答就好.可能应届生会抱怨设计题, ...
- SpringCloud IDEA 教学 (四) 断路器(Hystrix)
写在开始 在SpringCloud项目中,服务之间相互调用(RPC Remote Procedure Call —远程过程调用),处于调用链路底层的服务产生不可用情况时,请求会产生堆积使得服务器线程阻 ...
- 五:ResourceManager High Availability RM 高可用
RM有单点失败的风险,但是可以做HA. RMs HA通过master/standby这种结构实现,一个master是active的,其它standby是inactive的.可能通过命令行切换主备节点 ...
- 关于css的总结
写在前面 ,学好css,需要长期的推敲和积累 ,细节是不断完善的,逐渐形成自己的风格 让自己的css更加接近优雅. 下面来总结一些我觉得比较好的css代码风格 : 1. 一般网页中的背景 用 ...
- 单源最短路——dijkstra算法
Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. 问 ...
- 总结Canvas和SVG的区别
参考链接: 菜鸟教程 HTML5 内联SVG 经典面试题(讨论canvas与svg的区别) Canvas SVG 通过 JavaScript 来绘制 2D 图形 是一种使用 XML 描述 2D 图形的 ...