Median(二分+二分)

Median

 http://poj.org/problem?id=3579

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11225 Accepted: 4016

Description

Given N numbers, X1X2, ... , 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 = 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 X1X2, ... , XN, ( X≤ 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

POJ Founder Monthly Contest – 2008.04.13, Lei Tao

 

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 #include<map>
 5 #include<vector>
 6 #include<cmath>
 7 #include<string.h>
 8 #include<stdlib.h>
 9 #include<stack>
10 #include<queue>
11 #include<cstdio>
12 #define ll long long
13 const long long MOD=1000000007;
14 #define maxn 100005
15 using namespace std;
16 
17 int n;
18 int m;
19 int a[maxn];
20 
21 bool Check(int mid){
22     int sum=0;
23     int p;
24     for(int i=1;i<=n;i++){
25         p=upper_bound(a+1,a+n+1,a[i]+mid)-a;
26         sum+=p-i-1;//排除a[i]之前的那些元素,共有i+1;
27     }
28     if(sum>=m){
29         return true;
30     }
31     return false;
32 }
33 
34 int main(){
35     while(~scanf("%d",&n)){
36         m=n*(n-1)/2;
37         m=(m+1)/2;
38         for(int i=1;i<=n;i++){
39             scanf("%d",&a[i]);
40         }
41         sort(a+1,a+n+1);
42         ll L=0,R=1000000000,mid;
43         while(L<=R){
44             mid=L+R>>1;
45             if(Check(mid)){
46                 R=mid-1;
47             }
48             else{
49                 L=mid+1;
50             }
51         }
52         printf("%d\n",L);
53     }
54 
55 }
View Code

 

转载于:https://www.cnblogs.com/Fighting-sh/p/10054652.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/570099.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VNCServer在Linux下设置

1.检查vnc客户端和服务器是否已经安装&#xff1a; [rootcentos ~]$ rpm -q vnc-server package vnc-server is not installed 说明没有安装 运行yum install vnc-server进行安装 vnc-server-4.0-8.1 说明安装已经安装了 只是没开启服务而已 2. 将用户名称加入到配置…

Judy alpha 第七天

SCRUM 今天 debugger 部分由 Yu Xing 继续完成断点功能&#xff0c;由 Zhiqi Lin 开始实现查看变量功能。项目整体打包测试仍有困难&#xff0c;VS Code extension 的文档质量不佳&#xff0c;大家感到难以调试。成员昨日进展今日任务Zibo Wang协助打包、追踪开发进度协助打包、…

c++教程下载

孙鑫C视频教程 rmvb格式 全20CD完整版http://bbs.topsage.com/dispbbs.asp?boardID121&ID156290C视频教程 边用边学Visual C 6 (ASF格式)http://bbs.topsage.com/dispbbs.asp?boardID121&ID177500中山大学蔡培兴 C语言视频教程 全51讲 精品推荐 http://bbs.topsage.c…

Phaser3让超级玛丽实现轻跳、高跳及加上对应的跳跃声音

mario jumper在线测试地址&#xff1a;http://www.ifiero.com/uploads/phaserjs3/jumper/ 空格键&#xff1a;轻按&#xff1a;跳低 &#xff0c;长按&#xff1a;跳高键盘&#xff1a;--> 向右 , <-- 向左 请确保已打开电脑的音乐开关 var config {type: Phaser.AUTO,w…

linux下文件删除不了

在linux系统下当删除文件时提示无法操作&#xff0c;有可能是文件被使用了chattr改变了属性。可以使用lsattr命令来查看 [rootlinux236 root]# lsattr   ---i---------- ./1.txt   -------------- ./weiqi.ldif -------------- ./qi.schema 以上可以看出1.txt文件被添加了…

struts2配置问题

struts2.2.1之后的版本除了commons-fileupload-1.3、commons-io-2.2、freemarker-2.3.19、javassist-3.11.0.GA、ognl-3.0.6、struts2-core-2.3.16、xwork-core-2.3.16之外&#xff0c;还需要commons-lang3-3.1才能正常运行

linux IptabLex 攻击

以下摘自某位老兄的文章&#xff0c;遇到问题一样&#xff0c;不知道是否是新出的病毒IptabLex大家都知道&#xff0c;互联网每时每刻都会存在攻击的可能性&#xff0c;&#xff08;系统频繁发包&#xff0c;SYN攻击&#xff0c;等等&#xff09;安全问题是系统管理员不可忽视的…

MySQL各个版本区别

MySQL 的官网下载地址&#xff1a;http://www.mysql.com/downloads/在这个下载界面会有几个版本的选择:1. MySQL Community Server 社区版本&#xff0c;开源免费&#xff0c;但不提供官方技术支持。2. MySQL Enterprise Edition 企业版本&#xff0c;需付费&#xff0c;可以试…

GIT和GitHub的使用总结

目录 一、GIT初始化设置&#xff1a;创建密钥添加远程仓库用户名邮箱配置Clone远程仓库创建本地仓库代理设置自定义操作二、GIT基本操作添加到暂存区区提交删除版本回退撤销修改三、GIT分支操作创建分支分支合并分支推送分支绑定分支删除stash功能四、GIT标签操作本地操作远程操…

分析MySQL数据类型的长度

分析MySQL数据类型的长度 MySQL有几种数据类型可以限制类型的"长度"&#xff0c;有CHAR(Length)、VARCHAR(Length)、TINYINT(Length)、SMALLINT(Length)、MEDIUMINT(Length)、INT(Length)、BIGINT(Length)、FLOAT(Length, Decimals)、DOUBLE(Length, Decimals)和DECI…

Mysql partition by

一&#xff0c;看原表 select * from user; 二&#xff0c;查询同组年级最大的 select username ,SUBSTRING_INDEX( GROUP_CONCAT(age order by age desc) ,,,1) uuid from ( select uuid ,username ,password ,age from user )b GROUP BY uuid asc; 转载于:https://www.cn…

tomcat内存溢出,修改设置

问题描述&#xff1a; 1. java.lang.OutOfMemoryError: Java heap space JVM堆的设置是指java程序运行过程中JVM可以调配使用的内存空间的设置.JVM在启动的时候会自动设置Heap size的值&#xff0c;其初始空间(即-Xms)是物理内存的1/64&#xff0c;最大空间(-Xmx)是物理内存的…

Mysql导入大容量SQL文件数据问题

mysql在通过导入sql文件可能会出现下面二个问题:1.如果sql文件过大,会出现"MySQL server has gone away"问题;2.如果sql文件数据有中文,会出现乱码 解决问题:问题1:出现MySQL server has gone away"问题,是因为mysql默认的"max_allowed_packet"变量…

电脑的引导启动快捷键

组装机主板品牌笔记本品牌台式机主板品牌启动按键笔记本品牌启动按键台式机品牌启动按键华硕主板F8联想笔记本F12联想台式机F12技嘉主板F12宏基笔记本F12惠普台式机F12微星主板F11华硕笔记本ESC宏基台式机F12映泰主板F9惠普笔记本F9戴尔台式机ESC梅捷主板ESC或F12联想ThinkpadF…

swift 4.2 - 根据字符串 push指定控制器

俩个方法 1. 创建类写成 类方法 import UIKit /** 注释&#xff1a;获得VC* 1.字符串 和使用的控制器&#xff0c;直接跳转* 2.用过字符串获得对应VC*/ class JYGetPushVc: NSObject {/// 指定字符串VC跳转,设置titlestatic func pushVcByVcNameAndTitle(pushVcNameStr:String…

linux下IPTABLES配置详解

如果你的IPTABLES基础知识还不了解,建议先去看看.开始配置我们来配置一个filter表的防火墙.(1)查看本机关于IPTABLES的设置情况[roottp ~]# iptables -L -nChain INPUT (policy ACCEPT)target prot opt source destination Chain FORWARD (polic…

centos通过添加chrome源来安装chrome

在google网站下载chrome速度很慢&#xff0c;但是添加了chrome的源之后&#xff0c;就可以在终端通过yum install 来安装chrome了&#xff0c;会快很多。 首先在/etc/yum.repos.d里建立一个名为google.repo文件&#xff0c;然后复制以下代码到其中&#xff0c;然后保存&#xf…

鼠标滑过抖动图标

在线演示 本地下载 转载于:https://www.cnblogs.com/qixidi/p/10079608.html

centos7安装VLC播放器

1.安装eple 下载地址&#xff1a;https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm 2.安装nux-dextop下载地址&#xff1a;http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm3.执行yum update 4.…

每日站立会议 12.06

每日站立会议 今天我们开始了对button按钮中素菜部分代码的编写&#xff0c;我负责编写时代码的复审。转载于:https://www.cnblogs.com/grrd17s/p/10079973.html