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 ...
随机推荐
- Python3基础 file open+write 对不存在的txt进行创建与写入
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Github使用教程详解
官方网站:http://git-scm.com Git是目前世界上最先进的分布式版本控制系统(没有之一). Git有什么特点?简单来说就是:高端大气上档次! 一.Git安装 在Linux上安装Git ...
- 分布式系统一致性协议--Paxos算法
Paxos: Paxos算法背景介绍: Paxos算法是分布式技术大师Lamport提出的,主要目的是通过这个算法,让参与分布式处理的每个参与者逐步达成一致意见.用好理解的方式来说,就是在一个选举过程 ...
- jsp拾遗
JSP九大内置对象,七大动作,三大指令 https://blog.csdn.net/qq_34337272/article/details/64310849 JSP页面的静态包含和动态包含 https ...
- The destination you provided is not a full refname (i.e., starting with "refs/")
$ git push v5 v5/hotfix/5.1:hotfix/5.1-quartzerror: The destination you provided is not a full refna ...
- IIS Logs
日志路径 %SystemDrive%\inetpub\logs\LogFiles https://stackify.com/where-are-iis-log-files-located/ Where ...
- MVC ---- DBHelper.ttinclude
在通过T4模版引擎之基础入门 对T4有了初步印象后,我们开始实战篇.T4模板引擎可以当做一个代码生成器,代码生成器的职责当然是用来生成代码(这不是废话吗).而这其中我们使用的最普遍的是根据数据库生成实 ...
- github+hexo搭建博客
引言 之前用阿里云弹性web托管采用wordpress搭建的个人博客,经过我使用一段时间之后发现存在很多问题: 网站的响应速度非常慢,估计打开主页需要3-4s的时间,我经过搜索发现很多人都有这 ...
- Python day5_tuple元祖的常见方法1_笔记
# 初识元祖# 1.元祖的一级元素不能被修改,增加,删除,但可以查看,del tu[0]错的# 2.元祖最后一个元素后注意加‘,’,并没有区别,只是为了和方法中元素区别开li=[11,22,33,44 ...
- 动态规划-Largest Sum of Averages
2018-07-12 23:21:53 问题描述: 问题求解: dp[i][j] : 以ai结尾的分j个部分得到的最大值 dp[i][j] = max{dp[k][j - 1] + (ak+1 + . ...