2015 UESTC 搜索专题D题 基爷的中位数 二分
基爷的中位数
Time Limit: 20 Sec Memory Limit: 256 MB
题目连接
http://acm.uestc.edu.cn/#/contest/show/61
Description
现在,基爷希望聪明的你能用一个简单的程序求出这 C2N 个数的中位数!
Input
每组数据,第一行一个整数 N,第二行给出 N 个整数 X1,X2,...,XN ( |Xi|≤1,000,000,000; 3≤N≤100,000 )
Output
按要求输出中位数,每个数占一行。
Sample Input
1 3 2 4
3
1 10 2
Sample Output
8
HINT
当这 C2N 个数的个数为偶数 M 的时候,取第 ⌊M2⌋ 个最小的数作为中位数 ( 别问为什么,这就是基爷的中位数! )
题意
题解:
二分答案枚举中位数,然后再二分 a[i]-a[j]=二分的那个数的值在哪儿
nloglog的做法
nlog就要用two pointer去优化
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** ll a[maxn],n;
ll tmp; int main()
{
while(scanf("%lld",&n)!=EOF)
{
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
tmp=n*(n-)/;
sort(a,a+n);
ll l=,r=;
while(l+<r)
{
ll mid=(l+r)>>;
ll ans=;
for(int i=;i<n;i++)
{
ans+=a+n-lower_bound(a+i+,a+n,a[i]+mid);
}
if(ans<=tmp/)
r=mid;
else
l=mid;
}
printf("%lld\n",l);
} }
2015 UESTC 搜索专题D题 基爷的中位数 二分的更多相关文章
- 2015 UESTC 搜索专题C题 基爷与加法等式 爆搜DFS
基爷与加法等式 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...
- 2015 UESTC 搜索专题N题 韩爷的梦 hash
韩爷的梦 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descrip ...
- 2015 UESTC 搜索专题F题 Eight Puzzle 爆搜
Eight Puzzle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 ...
- 2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp
秋实大哥の恋爱物语 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 De ...
- 2015 UESTC 搜索专题E题 吴队长征婚 爆搜
吴队长征婚 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descri ...
- 2015 UESTC 搜索专题B题 邱老师降临小行星 记忆化搜索
邱老师降临小行星 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...
- 2015 UESTC 搜索专题M题 Palindromic String 马拉车算法
Palindromic String Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/s ...
- 2015 UESTC 搜索专题J题 全都是秋实大哥 kmp
全都是秋实大哥 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...
- 2015 UESTC 搜索专题A题 王之迷宫 三维bfs
A - 王之迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...
随机推荐
- 【IDEA】IDEA中配置tomcat虚拟路径的两种方法
首先要确保使用的是本地的tomcat服务器,而不是maven插件. -------------------------第一种:使用IDEA工具自动配置(推荐这种)------------------- ...
- makefile里PHONY的相关介绍
Phony Targets PHONY 目标并非实际的文件名:只是在显式请求时执行命令的名字.有两种理由需要使用PHONY 目标:避免和同名文件冲突,改善性能. 如果编写一个规则,并不产生目标文件 ...
- Python递归 — — 二分查找、斐波那契数列、三级菜单
一.二分查找 二分查找也称之为折半查找,二分查找要求线性表(存储结构)必须采用顺序存储结构,而且表中元素顺序排列. 二分查找: 1.首先,将表中间位置的元素与被查找元素比较,如果两者相等,查找结束,否 ...
- MINI_httpd移植,构建小型WEB服务器
一.简介 目的:构建小型WEB站,具备SSL. mini_httpd is a small HTTP server. Its performance is not great, but for low ...
- nginx配置tomcat集群
显示nginx的核心配置 #user nobody;worker_processes 1; events { worker_connections 1024; #并发连接数} http { in ...
- 1-4 TCP/IP协议族
网络协议是在内核中实现的,socket是对tcp/ip协议的系统调用,提供以下两点功能: 1. 将应用撑血数据从用户缓冲区中复制到TCP/UDP内核发送缓冲区,以交付内核发送来的数据(比如send), ...
- CentOS7 安装 chrome-gnome-shell
1.download https://gitlab.gnome.org/GNOME/chrome-gnome-shell 2.安装cmake和jq yum install -y cmake yum i ...
- ubuntu1604 安装samba
ubuntu1604 安装samba sudo apt-get install samba 更改/etc/samba/smb.conf添加[share]comment = sharepath = /h ...
- CodeForces 803D Magazine Ad
二分. 首先把字符串处理成一个数组,二分答案,判断一下即可. #include <cstdio> #include <cmath> #include <set> # ...
- XV6操作系统代码阅读心得(三):锁
锁是操作系统中实现进程同步的重要机制. 基本概念 临界区(Critical Section)是指对共享数据进行访问与操作的代码区域.所谓共享数据,就是可能有多个代码执行流并发地执行,并在执行中可能会同 ...