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的 ...
随机推荐
- EF Core中如何正确地设置两张表之间的关联关系
数据库 假设现在我们在SQL Server数据库中有下面两张表: Person表,代表的是一个人: CREATE TABLE [dbo].[Person]( ,) NOT NULL, ) NULL, ...
- 【visual studio code 的python开发环境搭建 】
打开vs code,按按F1或者Ctrl+Shift+P打开命令行,然后输入ext install 输入Python,选第一个,这个用的最多,支持自动补全代码等功能,点击安装按钮,即可安装 下面试着编 ...
- Redis笔记 -- 链表和链表节点的API函数(三)
链表和链表节点API 函数 作用 时间复杂度 listSetDupMethod 将给定的函数设置为链表的节点值复制函数 复制函数可以通过链表的dup属性直接获得,O(1) listGetDupMeth ...
- Flash的swf文件破解
1.准备好flash文件,xxx.swf(后缀为swf),将其重构swf文件为fla源文件. 2.asv软件(5以上版本)的操作 1.点击左上角 文件 --> 打开 --> 运行已准备好的 ...
- Delphi跨平台下的GetTickCount,GetCurrentThreadID
在Windows下只要uses Windows,就有这两个API可调用GetTickCount,GetCurrentThreadID 如果我们需要跨平台使用这两个函数,就不能仅仅Uses Window ...
- linux几个重要的组合键
我们在用Windows系统时,有没有感觉快键键让我们工作更有效率,在Linux系统中仍有很好用的快捷键,这些快捷键可以辅助我们进行指令的编写与程序的中断呢,下面介绍几个经常用到的快捷键. 一.Tab- ...
- python学习笔记:第10天 函数进阶和作用域
目录 1. 函数进阶 2. 命名空间 3. 关键字global和nonlocal 1. 函数进阶 动态接收位置参数 之前写的函数都是固定参数的,假设有个函数需要的参数由几十个,一个个写在形参的位置会非 ...
- vue相关理论知识
es6常用语法简介 es是js的规范标准 let 特点: 1.有全局和函数作用域,以及块级作用域(用{}表示块级作用域范围) 2.不会存在变量提升 3.变量不能重复声明 const 特点: 1.有块级 ...
- 记数据结构MOOC-二叉树
主要的学习内容 在本章中,主要学习了二叉树的实现以及各种遍历的方法.着重介绍了前序.中序.后序三种遍历方法的递归实现,同时也描述了前序中序遍历的迭代方法. 教材的主要内容 教材是以哈夫曼编码树为主要脉 ...
- 20155203 2016-2017-2《Java程序设计》课程总结
目录 一.每周作业链接汇总 自认为写得最好一篇博客是?为什么? 作业中阅读量最高的一篇博客是?谈谈经验 作业中与师生交互最多的一篇博客是?谈谈收获 二.实验报告链接汇总 三.代码托管链接 四.课堂项目 ...