poj 3579 Median (二分搜索之查找第k大的值)
- Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numbers: ∣Xi - Xj∣ ( ≤ i < j ≤ N). We can get C(N,) 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/)-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 = .
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 ≤ ,,, ≤ N ≤ ,, )
Output
- For each test case, output the median in a separate line.
Sample Input
Sample Output
Source
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<math.h>
- #include<stdlib.h>
- using namespace std;
- #define N 100006
- int n,m;
- int a[N];
- bool solve(int mid){
- int cnt=;
- for(int i=;i<n;i++){
- int tmp=n-(lower_bound(a,a+n,a[i]+mid)-a);//a[i]加上mid看看有多少个
- cnt+=tmp;
- }
- if(cnt>m) return true;//如果太多,则要调整mid向上,>还是>=有时候要调整
- return false;
- }
- int main()
- {
- while(scanf("%d",&n)==){
- m=n*(n-)/;
- for(int i=;i<n;i++){
- scanf("%d",&a[i]);
- }
- sort(a,a+n);
- int low=;
- int high=a[n-]-a[];
- while(low<high){
- int mid=(low+high)>>;
- if(solve(mid)){
- low=mid+;
- }
- else{
- high=mid;
- }
- }
- printf("%d\n",low-);//有时要调整,减1或不减
- }
- return ;
- }
poj 3579 Median (二分搜索之查找第k大的值)的更多相关文章
- POJ 3579 3685(二分-查找第k大的值)
POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...
- poj 3685 Matrix(二分搜索之查找第k大的值)
Description Given a N × N matrix A, whose element × i + j2 - × j + i × j, you are to find the M-th s ...
- 查找第K大的值
这种题一般是给定N个数,然后N个数之间通过某种计算得到了新的数列,求这新的数列的第K大的值 POJ3579 题意: 用$N$个数的序列$x[i]$,生成一个新序列$b$. 新的序列定义为:对于任意的$ ...
- POJ 3579 median 二分搜索,中位数 难度:3
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3866 Accepted: 1130 Descriptio ...
- poj 2579 中位数问题 查找第K大的值
题意:对列数X计算∣Xi – Xj∣组成新数列的中位数. 思路:双重二分搜索 对x排序 如果某数大于 mid+xi 说明在mid后面,这些数的个数小于 n/2 的话说明这个中位数 mid 太大 反之太 ...
- POJ_3579_Median_(二分,查找第k大的值)
描述 http://poj.org/problem?id=3579 给你一串数,共C(n,2)个差值(绝对值),求差值从大到小排序的中值,偶数向下取. Median Time Limit: 1000M ...
- POJ_3685_Matrix_(二分,查找第k大的值)
描述 http://poj.org/problem?id=3685 一个n*n的矩阵,(i,j)的值为i*i+100000*i+j*j-100000*j+i*j,求第m小的值. Matrix Time ...
- 基于快速排序思想partition查找第K大的数或者第K小的数。
快速排序 下面是之前实现过的快速排序的代码. function quickSort(a,left,right){ if(left==right)return; let key=partition(a, ...
- 如何用快排思想在O(n)内查找第K大元素--极客时间王争《数据结构和算法之美》
前言 半年前在极客时间订阅了王争的<数据结构和算法之美>,现在决定认真去看看.看到如何用快排思想在O(n)内查找第K大元素这一章节时发现王争对归并和快排的理解非常透彻,讲得也非常好,所以想 ...
随机推荐
- poj 3744 Scout YYF I (矩阵)
Description YYF -p. Here is the task, given the place of each mine, please calculate the probality t ...
- Django之验证码 + session 认证
验证码 + session认证 目录结构 . └── project ├── app01 │ ├── admin.py │ ├── apps.py │ ├── __init__.py │ ...
- IOS Layer的使用
CALayer(层)是屏幕上的一个矩形区域,在每一个UIView中都包含一个根CALayer,在UIView上的所有视觉效果都是在这个Layer上进行的. CALayer外形特征主要包括: 1.层的大 ...
- 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6564592 在前一篇文章提到,从源代码树下载下 ...
- c++11 NULL、0、nullptr
C的NULL 在C语言中,我们使用NULL表示空指针,也就是我们可以写如下代码: int *i = NULL;foo_t *f = NULL; 实际上在C语言中,NULL通常被定义为如下: #de ...
- C++11多线程教学II
从我最近发布的C++11线程教学文章里,我们已经知道C++11线程写法与POSIX的pthreads写法相比,更为简洁.只需很少几个简单概念,我们就能搭建相当复杂的处理图片程序,但是我们回避了线程同步 ...
- Java_.jar .war .ear 详解
.jar 全称: java archive: 包含: class.properties文件,是文件封装的最小单元: ...
- Internetmap.apk实现原理分析
1.本地实现调用 程序根据data文件目录下的asinfo.json文件(包含自治域网络名和对应的坐标值),调用so文件绘制asn结点图(ASN,AutoSystemNode,自治域结点) 2.路由查 ...
- Linux目录结构及文件基础操作
一.Linux目录结构 windows系统中主要以存储介质为主(磁盘). UNIX和Linux系统中主要是以目录为主(以树形目录结构的形式构建整个系统). 1. FHS(Filesystem Hier ...
- win7_64位主机装虚机Linux系统(VMware Workstation10+CentOS6.5)详细步骤图文讲解
第一步:创建新的虚拟机 第二步:选择“典型”安装 第三步:选择映像文件安装—浏览选择iso文件 第四步:选择稍后安装操作系统 第五步:系统选择Linux,版本选择centOS64位 第六步:虚拟机名称 ...