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的 ...
随机推荐
- 允许跨域资源共享(CORS)携带 Cookie (转载)
如何让CORS携带Cookie CORS 是一个 W3C 标准,全称是“跨域资源共享”(Cross-origin resource sharing).默认浏览器为了安全,遵循“同源策略”,不允许 Aj ...
- C#中调用方法
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- mysql数据库锁简介
本篇介绍有关数据库锁相关的知识,关于数据库事务及隔离级别参见<数据库事务ACID特性及隔离级别>这篇文. 乐观锁 乐观锁最常用的实现方式是用数据版本(Version)记录机制.数据版本 ...
- React Native封装Toast与加载Loading组件
React Native开发封装Toast与加载Loading组件 在App开发中,我们避免不了使用的两个组件,一个Toast,一个网络加载Loading,在RN开发中,也是一样,React Nati ...
- MySQL学习之路(一)——初涉MySQL。
MySQL学习之路(一) 1.1MySQL的概述 MySQL由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一个开源的关系型数据库管理系统. MySQL分为社区版和企业版. 1 ...
- mongdb的聚合管道
我们先介绍一下 MongoDB 的聚合功能,聚合操作主要用于对数据的批量处理,往往将记录按条件分组以后,然后再进行一系列操作,例如,求最大值.最小值.平均值,求和等操作.聚合操作还能够对记录进行复杂的 ...
- Titanic幸存预测分析(Kaggle)
分享一篇kaggle入门级案例,泰坦尼克号幸存遇难分析. 参考文章: 技术世界,原文链接 http://www.jasongj.com/ml/classification/ 案例分析内容: 通过训练集 ...
- 课下测试CH01补交
课下测试CH01补交 ( 单选题 | 1 分) Amdahl定律说明,我们对系统的某个部分做出重大改进,可以显著获得一个系统的加速比. A . 正确 B . 错误 正确答案: B 你的答案: 未作答 ...
- 关于正则表达式"\b"
今天刚刚开始看正则表达式就遇到一个十分头疼的问题,原文是这样的: “不幸的是,很多单词里包含hi这两个连续的字符,比如him,history,high等等.用hi来查找的话,这里边的hi也会被找出来. ...
- #ifdef 支持Mac #ifndef 支持Windows #if defined (Q_OS_WIN) 应该可以再两个系统通用
//mac qt可以运行 #ifdef Q_OS_MAC qDebug()<<QSysInfo::MacintoshVersion; #endif //Mac不运行 #ifndef Q_O ...