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 ...
随机推荐
- C:gcc的基本使用
分布编译 预处理:gcc -E hello.c -o hello.i 编 译:gcc -S hello.i -o hello.s 汇 编:gcc -c hello.s -o hello.o 链 接:g ...
- vs的一些操作技巧:在写代码时自动换行的设置
有时在写代码的时候,一行代码太长了,想换行,直接按回车键的话又会报错,怎么办?其实可以这样设置vs,就可以达到自动换行的效果啦.
- ArrayList,LinkedList,vector的区别
1,Vector.ArrayList都是以类似数组的形式存储在内存中,LinkedList则以链表的形式进行存储. 2.List中的元素有序.允许有重复的元素,Set中的元素无序.不允许有重复元素. ...
- Linux OS 集群 免密登录
1. ssh-keygen 生成密钥 2. ssh-copy-id 集群主机名 参考: [图文详解]linux下配置远程免密登录
- leetcode 0211
目录 ✅ 1217. 玩筹码 描述 解答 c java py ✅ 206. 反转链表 描述 解答 c java py ✅ 922. 按奇偶排序数组 II 描述 解答 c 双指针soldier tddo ...
- 构造数列Huffman树总耗费_蓝桥杯
快排! /** 问题描述 Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, …, pn-1},用这列数构造Huffman树的 ...
- 前端开发:javascript中的面向对象
前端开发:面向对象与javascript中的面向对象实现(一) 面向对象理解: 面向对象是一种对现实世界理解和抽象的方法,是一种先进的程序设计理念,是一种比较抽象的,多形态的设计模式.我们可以这么理解 ...
- Linux搭建maven私服
1.把压缩包上传到服务器/usr/local/tmp 2.在/usr/local下创建nexus文件夹(mkdir nexus) 3.解压压缩包nexus-3.13.0-01-unix.tar.gz到 ...
- Python 基础之压缩模块zipfile与tarfile
一.压缩模块 zipfile (1)创建一个zip压缩包 import zipfile #zip_deflated 代表是压缩的意思#打开压缩包zf = zipfile.ZipFile("c ...
- Python经典排序算法
https://www.cnblogs.com/onepixel/p/7674659.html这个文章很nice https://www.bilibili.com/video/av685670?fro ...