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 ...
随机推荐
- POJ 1704 Georgia and Bob(阶梯博弈)题解
题意:有一个一维棋盘,有格子标号1,2,3,......有n个棋子放在一些格子上,两人博弈,只能将棋子向左移,不能和其他棋子重叠,也不能跨越其他棋子,不能超越边界,不能走的人输 思路:可以用阶梯博弈来 ...
- HDU 2841 Visible Trees(容斥)题解
题意:有一块(1,1)到(m,n)的地,从(0,0)看能看到几块(如果两块地到看的地方三点一线,后面的地都看不到). 思路:一开始是想不到容斥...后来发现被遮住的地都有一个特点,若(a,b)有gcd ...
- 【附12】grafana配置文件
一.配置文件位置 /usr/local/opt/grafana/share/grafana/default.ini /usr/local/etc/grafana/grafana.ini 二.init格 ...
- [js] - 关于js的排序sort
js的排序sort并不能一次排序好 function solution(nums){ return nums.sort(sortNumber); } function sortNumber(a, b) ...
- [SpringBoot] - 发送带附件的邮件
<!--发送email依赖--> <dependency> <groupId>org.springframework.boot</groupId> &l ...
- CenterOS下从零起步简单部署RockMongo
使用Mongodb,对于调试Query,查看Collection等状态,有Rockmongo是非常方便的. 研究了下Rockmongo的部署,主要是依赖PHP环境的web服务器,当前有两种服务器,一种 ...
- C# WinCE项目 VS2008 单例窗体实现
项目现有主界面FormMain,模板界面FormModel,其余5个子界面皆继承自模板. 现在想要实现在主界面下可以打开任意子界面,并且可以随时关闭.当打开的子窗体未执行Close事件时,要保证每次显 ...
- UVa 10766 Organising the Organisation(矩阵树定理)
https://vjudge.net/problem/UVA-10766 题意: 给出n, m, k.表示n个点,其中m条边不能直接连通,求生成树个数. 思路: 这也算个裸题,把可以连接的边连接起来, ...
- linux中批量替换文本中字符串--转载
(一)通过vi编辑器来替换.vi/vim 中可以使用 :s 命令来替换字符串.:s/well/good/ 替换当前行第一个 well 为 good:s/well/good/g 替换当前行所有 well ...
- python 判断一个数字是否为3的幂
def is_Power_of_three(n): == ): n /= ; ; print(is_Power_of_three()) print(is_Power_of_three()) print ...