POJ3579 Median —— 二分
题目链接:http://poj.org/problem?id=3579
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8286 | Accepted: 2892 |
Description
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
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; int n, a[MAXN];
int m; bool test(int mid)
{
int cnt = ;
for(int i = ; i<=n; i++) //注意,对于每个数,只需往一边找,否则会出现重复计算。
cnt += upper_bound(a+i+, a++n, a[i]+mid)-(a+i+); //在[i+1, n+1)的范围,即[i+1,n]
return cnt>=m;
} int main()
{
while(scanf("%d", &n)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d", &a[i]); sort(a+, a++n);
m = n*(n-)/; //有多少个数
m = (m+)/; //中位数所在的位置
int l = , r = a[n]-a[];
while(l<=r)
{
int mid = (l+r)>>;
if(test(mid))
r = mid - ;
else
l = mid + ;
}
printf("%d\n", l);
}
}
POJ3579 Median —— 二分的更多相关文章
- POJ3579 Median
Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...
- poj3579 二分搜索+二分查找
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5468 Accepted: 1762 Descriptio ...
- POJ 3579 Median 二分加判断
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12453 Accepted: 4357 Descripti ...
- POJ 3579 Median (二分)
...
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- C. Maximum Median 二分
C. Maximum Median 题意: 给定一个数组,可每次可以选择一个数加1,共执行k次,问执行k次操作之后这个数组的中位数最大是多少? 题解:首先对n个数进行排序,我们只对大于中位数a[n/2 ...
- Median(二分+二分)
Median http://poj.org/problem?id=3579 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1 ...
- 【POJ - 3579 】Median(二分)
Median Descriptions 给N数字, X1, X2, ... , XN,我们计算每对数字之间的差值:∣Xi - Xj∣ (1 ≤ i < j ≤N). 我们能得到 C(N,2) 个 ...
- POJ 3579 Median(二分答案)
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11599 Accepted: 4112 Description G ...
随机推荐
- Linux(8):linux三剑客sed和awk & Shell 编程(1)
linux 三剑客 之 sed # sed 是什么? # sed : 字符流编辑器 Stream Editor: sed 擅长 替换.取行等 # sed 的功能与版本: 处理纯文本文件.日志.配置文件 ...
- Python脚本实现单据体首行过滤
编写的Python脚本 可以看到,实际代码只有3句,即实现单据体首行过滤代码(其实最最主要的是无需写组件动态即时注册),并有注册到[采购订单]"表单构建插件"上.界面运行时,实际效 ...
- uva 11991 查询中容器的运用
题目大意:一个n个整数的数组,m条查询指令.(1<=n,m<=100 000)每次询问第k个整数v的下标值,若不存在输出0. #include<iostream> #inclu ...
- H5 折线图插件
一.可以使用Highcharts,参考网址:https://api.hcharts.cn/highcharts: 二.可以使用Echarts,参考网址:http://echarts.baidu.com ...
- Spring注解处理Ajax请求-JSON格式[系统架构:Spring+SpringMVC+MyBatis+MySql]
1.前端jsp页面 <div class="tab_tip"> 请输入[身份证号或姓名] <input type="text" class=& ...
- T3054 高精度练习-文件操作 codevs
http://codevs.cn/problem/3054/ 题目描述 Description 输入一组数据,将每个数据加1后输出 输入描述 Input Description 输入数据:两行,第 ...
- SpringBoot中mybatis的自动生成
1.在pom文件中加入自动生成的插件 <!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybat ...
- mysql获取子父级节点
获取所有子节点 DROP FUNCTION IF EXISTS `F_Co29_GetAllChildrenIdsOfTaskevent`;DELIMITER //CREATE FUNCTION `F ...
- paramiko使用exec_command执行rm -rf删除目录的坑
paramiko删除目录后的上传操作请参考步骤1.2.3的说明 try: ssh = SSHClient(ip,user) sftpClient = ssh.getSftpClient() outpu ...
- kibana dev tools快捷键
kibana dev tools快捷键 ctrl+enter 提交请求 ctrl+i 自动缩进 ctrl+enter 提交请求 down 打开自动补全菜单 enter或tab 选中项自动补全 esc ...