POJ 3579:Median 差值的中位数
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 4680 | Accepted: 1452 |
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
题意是给出一个数组,然后这些数组元素每一对之间都有一个差值,找出这些差值的中位数。
两次二分,第一次二分是枚举答案,第二次二分是我觉得很好玩的地方,在给定的差值下,找到多少个对的差值小于等于它,用这样的二分来判断这个差值在所有的差值中的位置。感觉很巧妙~
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int num,m;
int val[100005]; bool check(int mid)
{
int i,sum=0; for (i = 0; i < num; i++)
{
sum += (lower_bound(val + i, val + num, val[i] + mid + 1) - (val + i) - 1);
}
if (sum >= m)
{
return true;
}
else
{
return false;
}
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i,left,right,mid;
while (scanf("%d", &num) != EOF)
{
for (i = 0; i < num; i++)
{
scanf("%d", &val[i]);
}
m = (num*(num - 1) / 2 + 1) / 2;
sort(val,val+num); left = 0;
right = val[num - 1] - val[0]; while (right-left > 1)
{
mid = (left + right) / 2; if (check(mid))
{
right = mid ;
}
else
{
left = mid ;
}
}
cout << right << endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3579:Median 差值的中位数的更多相关文章
- 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 (二分)
...
- 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 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- POJ 3579 Median 二分加判断
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12453 Accepted: 4357 Descripti ...
- POJ 3579 3685(二分-查找第k大的值)
POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...
随机推荐
- 最全BT磁力搜索引擎,国外最受欢迎的BT-磁力网站(整理分享,每日不断更新...)
最全BT磁力搜索引擎索引(整理分享,每日更新) 1.海盗湾 The Pirate Bay 2.磁力天堂(BT磁力搜索下载-磁力天堂) www.btaa.xyz (资源多,下载速度可以,建议用手机访问 ...
- 操作系统OS,Python - 协程(Coroutine)
留坑 参考: https://en.wikipedia.org/wiki/Coroutine https://zh.wikipedia.org/wiki/%E5%8D%8F%E7%A8%8B http ...
- 设计模式01 创建型模式 - 原型模式(Protype Pattern)
参考 1. 设计模式:原型模式 | 博客园 2. Java clone深拷贝.浅拷贝 | CSDN 3. Cloneable接口和Object的clone()方法 | 博客园 原型模式(Prototy ...
- BigOps自动化运维安装以及所遇故障处理
本文参考官方文档进行安装,以及在安装中所遇到的问题呈现给大家.废话就不说了,开始安装.一.准备工作:本机系统环境是CentOS 7 x86 64位硬件配置建议物理内存8G+.CPU 4 cores+. ...
- Go的WaitGroup
goroutine使用方便,但是如果不加以处理一般会deadlock,因为goroutine配合Chanel的话只能是一进一出,否则就会卡在那里.下面一个示例就是利用这个WaitGroup处理这种死锁 ...
- IELTS Writing Task 2: 'music' essay
IELTS Writing Task 2: 'music' essay Here's my band 9 sample answer for the question below. Some peop ...
- Python学习第十课——文件的基本操作
文件基本操作 文件读操作 #读出路径下的测试.txt文件 f = open('测试.txt', encoding='utf-8') # 打开要读文件 data = f.read() # 读取内容 pr ...
- 用C/C++创建windows服务程序
转载:https://blog.csdn.net/chenyujing1234/article/details/8023816 一.演示过程下方代码演示了如何使用vs(C/C++)创建windows服 ...
- 【PAT甲级】1027 Colors in Mars (20 分)
题意: 输入三个范围为0~168的整数,将它们从十三进制转化为十进制然后前缀#输出. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ...
- 六、Centos7中配置svn服务器
今天配置了 SVN 记在这儿 备忘: --svn开机自启动服务 systemctl enable svnserve.service --svn开机自启动服务 systemctl disable svn ...