POJ 3579 Median 二分加判断
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 12453 | Accepted: 4357 |
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
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long const int maxn = 1e5+;
int n;
ll m;
int arr[maxn];
bool valid(int mid){
int cnt = ;
for(int i=;i<n;i++){
cnt += (upper_bound(arr+i,arr+n,arr[i]+mid)--(arr+i));
}
return cnt >= m;
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=;i<n;i++)
scanf("%d",&arr[i]);
sort(arr,arr+n);
m = 1ll*n*(n-)/;
if(m%==){
m = m/;
}else m = m/+;
int l = -, r = arr[n-]-arr[];
while(l <= r){
int mid = (l+r)/;
if(valid(mid))
r = mid-;
else
l = mid+;
}
printf("%d\n",l);
}
return ;
}
POJ 3579 Median 二分加判断的更多相关文章
- POJ 3579 Median (二分)
...
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- 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 【二分答案】
<题目链接> 题目大意: 给出 N个数,对于存有每两个数的差值的序列求中位数,如果这个序列长度为偶数个元素,就取中间偏小的作为中位数. 解题分析: 由于本题n达到了1e5,所以将这些数之间 ...
- poj 3579 Median (二分搜索之查找第k大的值)
Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...
- POJ 3579 median 二分搜索,中位数 难度:3
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3866 Accepted: 1130 Descriptio ...
- POJ 3579 二分
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7687 Accepted: 2637 Descriptio ...
- POJ 3579 3685(二分-查找第k大的值)
POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...
随机推荐
- css 写打印样式问题
(1)背景颜色打印不出来问题解决方法 background样式要加上 !important: color样式要加上 !important: -webkit-print-color-adjust: ...
- Xcode 控制台打印Unicode字符串转换为中文
在Xcode的控制台里直接打印一个数组或者字典,输出的都是一些Unicode的编码,不方便调试. 要想看到中文,则要去获取对应的key或者数组下标.得到具体某一个对象才能看到中文,给我们调试起来 ...
- TensorFlow的前世和今生
TensorFlow的前世和今生 TensorFlow是一个开放源码的软件库,用于跨一系列任务的数据流处理编程.TensorFlow是一个符号化的数学应用库,广泛用于机器学习,例如神经网络.在谷歌公司 ...
- docker 入门 (一)重要概念介绍
序 之前一直想学一下docker,但是基本看完就忘记了,下次用还是要再翻一下教程. 最近项目要用,连续用了一段时间,就熟练了,基本的使用以及概念都记住了. 趁现在还刚入门,就把入门一些容易遇到的问题记 ...
- Mongo DB命令简介
引言 最近在学习MongoDB 总结了一些命令及常用的东西做整理 常用目录文件介绍 mongod 数据库部署命令 mongo 连接mongodb数据库而使用的命令 mongoimport 导入 ...
- 截图:【炼数成金】深度学习框架Tensorflow学习与应用
创建图.启动图 Shift+Tab Tab 变量介绍: F etch Feed 简单的模型构造 :线性回归 MNIST数据集 Softmax函数 非线性回归神经网络 MINIST数据集分类器简单版 ...
- vue项目持久化存储数据的实现代码
方式一.使用localStorage在数据存储 1.要在浏览器刷新的时候重新存储起来 if (window.localStorage.getItem(authToken)) { store.commi ...
- EOS节点远程代码执行漏洞细节
这是一个缓冲区溢出越界写漏洞 漏洞存在于在 libraries/chain/webassembly/binaryen.cpp文件的78行, Function binaryen_runtime::ins ...
- RandomAccessFile java
RandomAccessFile 用来支持读写随机存取文件的类.提供“文件指针”,类似于游标和下标,使用getFilePointer()方法获得,利用seek()方法设置下标. public Rand ...
- leetcode记录-回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...