Day3-O-Median POJ3579
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 思路:直接N^2肯定行不通,那么我们可以二分差值,满足单调且可验证,最小最大问题,代码如下:
const int maxm = ; int n, buf[maxm], m; bool check(int d) {
int sum = ;
for (int i = ; i < n; ++i) {
sum += upper_bound(buf + i, buf + n, buf[i] + d) - buf - i - ;
}
return sum >= m;
} int main() {
while(scanf("%d",&n) != EOF) {
for (int i = ; i < n; ++i)
scanf("%d", &buf[i]);
sort(buf, buf + n);
m = n * (n - ) / ;
if(m % == )
m = m / ;
else
m = m / + ;
int l = , r = buf[n - ] - buf[], mid;
while(l <= r) {
mid = (l + r) >> ;
if(check(mid))
r = mid - ;
else
l = mid + ;
}
printf("%d\n", l);
}
return ;
}
Day3-O-Median POJ3579的更多相关文章
- POJ3579 Median
Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...
- POJ3579 Median —— 二分
题目链接:http://poj.org/problem?id=3579 Median Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- 【POJ - 3579 】Median(二分)
Median Descriptions 给N数字, X1, X2, ... , XN,我们计算每对数字之间的差值:∣Xi - Xj∣ (1 ≤ i < j ≤N). 我们能得到 C(N,2) 个 ...
- No.004:Median of Two Sorted Arrays
问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...
- [LeetCode] Find Median from Data Stream 找出数据流的中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- Applying vector median filter on RGB image based on matlab
前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...
- 【leetcode】Median of Two Sorted Arrays
题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...
- Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...
- 【leedcode】 Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
随机推荐
- WLC HA模式下的注意事项
管理控制器:1.控制器默认开启的是SSH (CLI),Secure Web/https (GUI)2.登录控制器的管理地址为Active设备所控制(主备的配置同步,所以管理地址一致)3.WLC HA状 ...
- onclick="this.src=this.src+'?'"是什么意思?
onclick="this.src=this.src+'?'" 这是表示当前图片链接 在当前链接值的基础上添加了一个问号 譬如当前src="check.aspx" ...
- 开关机安全控制!(设置进入bois的密码)
1.调整 BOIS 引导设置(1)将第一引导设备设为当前系统所在硬盘 (2)设置管理员密码 (3)进入bois后如图所示需输入bols密码才能登入
- android 支持上拉加载,下拉刷新的列表控件SwipeRefreshLayout的二次封装
上拉加载,下拉刷新的列表控件,大家一定都封装过,或者使用过 源代码,我会在最后贴出来 这篇代码主要是为了解决两个问题 1.滑动冲突得问题 2.listview无数据时,无数据布局的展示问题 下方列出的 ...
- extractvalue报错注入
查看源码 $uagent = $_SERVER['HTTP_USER_AGENT']; ………… $uname = check_input($_POST['uname']); $passwd = ch ...
- shell coding about mac ox
1, mac path---http://blog.csdn.net/playstudy/article/details/50149021 Mac系统的环境变量,加载顺序为:/etc/profile ...
- CentOS7.3下载地址
CentOS 7.3,是CentOS-7系列的第四个发行版本,官方版本号为7.3.1611.该版本的安装映像只有 64 位,具体的安装映像有以下几种: DVD版 推荐(迅雷下载):http://arc ...
- 3 JavaScript正则表达式
正则表达式:Regular(有规则的) Expression 正则表达式是由一个字符序列形成的搜索模式,可用于文本搜索和文本替换 常见于字符串的search和replace方法 var str = & ...
- Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)
原文:http://blog.csdn.net/liuyiming_/article/details/7704923 SharedPreferences介绍: SharedPreferences是An ...
- 「Luogu P2468 [SDOI2010]粟粟的书架」
这道题分为两个部分 Part1 前置芝士 前缀和(后缀和,二维前缀和):可以预处理一下数据. 二分查找:可以在较短的时间内找出答案. 具体做法 可以发现\(R,C\)不大,只有\(200\),于是可以 ...