【POJ - 3579 】Median(二分)
Median
Descriptions
给N数字, X1, X2, ... , XN,我们计算每对数字之间的差值:∣Xi - Xj∣ (1 ≤ i < j ≤N). 我们能得到 C(N,2) 个差值,现在我们想得到这些差值之间的中位数。
如果一共有m个差值且m是偶数,那么我们规定中位数是第(m/2)小的差值。
Input
输入包含多测
每个测试点中,第一行有一个NThen N 表示数字的数量。
接下来一行由N个数字:X1, X2, ... , XN
( Xi ≤ 1,000,000,000 3 ≤ N ≤ 1,00,000 )
Output
对于每个样例,输出中位数即可。
Sample Input
4
1 3 2 4
3
1 10 2
Sample Output
1
8
题目链接
https://vjudge.net/problem/POJ-3579
注意 这里不能直接把所有数的差都保存下来,直接输出,这样数组会爆掉,我卡了好长时间
老老实实用二分去求中位数把
C2n的求法是 n*(n-1)/2,即一共这么多差 中间那个差也就是中位数就是n*(n-1)/4
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 100000+5
using namespace std;
ll n,m;
ll l,r,mid;
ll a[Maxn];
int judge(int x)
{
ll cnt=;//统计小于x的差有多少
for(int i=; i<n; i++)
cnt+=n-(lower_bound(a,a+n,a[i]+x)-a);
return cnt>m;//大于一半
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
m=n*(n-)/;
for(int i=; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
l=,r=a[n-]-a[];
while(r-l>)//如果是 r>l的话,因为是整除最后会卡死
//举个例子 l=1,r=2 块就会被卡死
{
mid=(l+r)/;
if(judge(mid))
l=mid;
else
r=mid;
}
printf("%d\n",l);
}
return ;
}
【POJ - 3579 】Median(二分)的更多相关文章
- 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 ...
- POJ 3579 Median(二分答案+Two pointers)
[题目链接] http://poj.org/problem?id=3579 [题目大意] 给出一个数列,求两两差值绝对值的中位数. [题解] 因为如果直接计算中位数的话,数量过于庞大,难以有效计算, ...
- POJ 3579 Median(二分答案)
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11599 Accepted: 4112 Description G ...
- POJ 3579 Median 【二分答案】
<题目链接> 题目大意: 给出 N个数,对于存有每两个数的差值的序列求中位数,如果这个序列长度为偶数个元素,就取中间偏小的作为中位数. 解题分析: 由于本题n达到了1e5,所以将这些数之间 ...
- poj 3579 Median (二分搜索之查找第k大的值)
Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...
- POJ 3579 median 二分搜索,中位数 难度:3
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3866 Accepted: 1130 Descriptio ...
- POJ 3579 3685(二分-查找第k大的值)
POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...
- POJ3579 Median —— 二分
题目链接:http://poj.org/problem?id=3579 Median Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
随机推荐
- Filter和interceptor比较
作为一个备忘,有时间补充 https://www.cnblogs.com/learnhow/p/5694876.html 先说一个题外话,Filter是过滤器,interceptor是拦截器.前者基于 ...
- Educational Codeforces Round 33 (Rated for Div. 2) A题
A. Chess For Three Alex, Bob and Carl will soon participate in a team chess tournament. Since they a ...
- frontEnd(前端基础)
第一章:前端概述 第二章:前端三剑客 第三章:第一个页面 第四章:html常用标签 第五章:标签分类 第六章:css三种引入方式 第七章:样式与长度颜色 第八章:常用样式 第九章:CSS选择器 第十章 ...
- Appium基础教程
目录 Appium教程 Appium简介 App自动化测试工具对比 Appium实现原理 环境搭建 Andorid介绍 基本架构 常见布局/视图 基本控件 控件常见属性 Adb介绍 Adb常用命令 A ...
- JS如何实现在微信中调用外部浏览器打开指定链接
使用方法,复制以下贴到index.php 顶部就可以了.场景 比如网页包含视频播放 在QQ\WX打开QQ\WX直接调用自己的播放器播放,而且播放完成还有AD推送,非常恶心,所以有了以下代码有乱码的话 ...
- sublime中替换成换行
ctrl + h 打开替换窗口. replace with里输入 ctrl + shift + enter
- 01 关于jupyter的环境安装
jupyter notebook环境安装 一.什么是Jupyter Notebook? 1. 简介 Jupyter Notebook是基于网页的用于交互计算的应用程序.其可被应用于全过程计算:开发 ...
- 【Spring Boot】 Spring Boot 2.x 版本 CacheManager 配置方式
Spring Boot 1.X RedisCacheManager 配置方式 @Bean public CacheManager cacheManager(RedisTemplate redisTem ...
- Leetcode题目56.合并区间(中等)
题目描述: 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 ...
- Java web 实验三部分资料上传
花好月圆系列 貂蝉 黄月英 孙尚香 甄姬 标准包 魏 曹操 司马懿 郭嘉 甄姬 张辽 许褚 夏侯惇 蜀 刘备 关羽 张飞 诸葛亮 黄月英 赵云 马超 吴 孙权 孙尚香 周瑜 大乔 甘宁 吕蒙 群 吕布 ...