POJ:3579-Median(二分+尺取寻找中位数)
Median
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9201 Accepted: 3209
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
中文题意:
给N数字, X1, X2, … , XN,我们计算每对数字之间的差值:∣Xi - Xj∣ (1 ≤ i < j ≤ N). 我们能得到 C(N,2) 个差值,现在我们想得到这些差值之间的中位数。
如果一共有m个差值且m是偶数,那么我们规定中位数是第(m/2)小的差值。
输入包含多测
每个测试点中,第一行有一个NThen N 表示数字的数量。
接下来一行由N个数字:X1, X2, … , XN
( Xi ≤ 1,000,000,000 3 ≤ N ≤ 1,00,000 )
解题心得:
- 首先要知道的是n个数字可以得到C(N,2)个数字,那么要得到中位数,如果是偶数就要得到第n/2大的数字,如果是奇数就要得到第n/2+1个数字。
- 具体做法可以先将给出的数字排序,然后每次二分枚举一个中位数(O(logn)),然后验证比这个中位数小的数字有多少个,在验证比这个中位数小的数字有多少个的时候可以选择尺取法(具体实现看代码,O(n)),这样就在O(nlogn)的复杂度内完成。
#include <algorithm>
#include <stdio.h>
#include <cstring>
using namespace std;
const int maxn = 1e5+100;
int n,num[maxn];
long long tot;
void init() {
tot = 0;
tot = (long long)n*(n-1)/2;//差值的个数
if(tot%2 == 0) {//中位数在第几个
tot /=2;
}
else
tot = tot/2 + 1;
for(int i=0;i<n;i++) {
scanf("%d",&num[i]);
}
sort(num,num+n);
}
bool checke(int va) {
int t = 0;
long long sum = 0;
for(int i=0;i<n;i++) {
while(t < n && num[t] - num[i] <= va)//尺取法看比va小的值有多少个
t++;
sum += t-i-1;
}
if(sum < tot)
return true;
return false;
}
int binary_search() {//每次枚举一个中位数的值
int l,r;
l = 0, r = 1e9+100;
while(r - l > 1) {
int mid = (l + r) >> 1;
if(checke(mid))
l = mid;
else
r = mid;
}
return r;
}
int main() {
while(scanf("%d",&n ) != EOF) {
init();
int ans = binary_search();
printf("%d\n",ans);
}
return 0;
}
POJ:3579-Median(二分+尺取寻找中位数)的更多相关文章
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- POJ 3579 Median 二分加判断
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12453 Accepted: 4357 Descripti ...
- POJ 3579 Median (二分)
...
- Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】
任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...
- hdu 6231 -- K-th Number(二分+尺取)
题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...
- POJ 3061 Subsequence ( 二分 || 尺取法 )
题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用 ...
- POJ 2566 Bound Found 尺取 难度:1
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1651 Accepted: 544 Spec ...
- POJ:2566-Bound Found(尺取变形好题)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5408 Accepted: 1735 Special J ...
- Subsequence (POJ - 3061)(尺取思想)
Problem A sequence of N positive integers (10 < N < 100 000), each of them less than or equal ...
随机推荐
- 关于 header()前面 为什么不能有任何输出的问题
之前有个问题就是在header () 之前输出,并没有任何报错信息. header() 官网定义必须在任何实际输出之前调用,不管是普通的HTML标签,还是文件或PHP输出的空行,空格.在测试的时候发现 ...
- php支付走过的坑(微信篇 包含h5支付和app支付 注册 秘钥 环境等等配置)
支付这东西,说容易也容易,说难也难 代码这玩意还比较好说 但是 如果没有demo 直接去看官方文档 十有八九一脸懵逼 今天就整理一下 支付这块走过的坑 涉及 微信h5支付 支付宝h5支付 (api文档 ...
- <Android 基础(六)> ActionBar
介绍 Action Bar是一种新増的导航栏功能,在Android 3.0之后加入到系统的API当中,它标识了用户当前操作界面的位置,并提供了额外的用户动作.界面导航等功能.使用ActionBar的好 ...
- 什么是TOPO学
拓扑,一个跟门萨同样古怪的“科技Word”.其定义,对绝大多数读者而言,不一定需要理解,但无妨知道———拓扑学,数学的一门分科,研究几何图形在一对一的双方连续变换下不变的性质.不少门萨题,来自拓扑学, ...
- Linux 查看CPU、Memory等资源占用情况
linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合: ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head linux下获取占用 ...
- 【Troubleshooting 】Outlook 客户端无法显示电子邮件图像
出于安全原因,Outlook 2013/2016不会在电子邮件中显示图像,但您可以右键单击图像并选择" 下载图片 "选项.我最近注意Outlook停止显示图像,并且没有下载图像的选 ...
- centos6.5_64bit-kvm安装部署
kvm部署安装 目录 kvm部署安装... 1 一.kvm部署... 1 1.关闭selinux和防火墙... 1 2.查看主机是否支持虚拟化... 1 3.安装kvm和其他虚拟化软件包... 1 ...
- UE4的蓝图都能做什么
创建关卡脚本 蓝图具有和虚幻3中Kismet一样的功能,每个关卡都由自己的蓝图,他可以: 引用和操控actors 控制使用Matinee的过场 管理关卡流,存档点以及其他关卡相关的系统 和关卡中的类蓝 ...
- java ——String , StringBuffer, StringBuilder类
一.String类概述 1.String对象一旦创建就不能改变. 2.字符串常量池. 字符串常量池的特点:池中有则直接使用,池中没有则创建新的字符串常量. 例1: “==” 比较两个对象是否引用同一 ...
- "提取位于北坡的各类用地面积信息"的程序设计与实现
"提取位于北坡的各类用地面积信息"的程序设计与实现 程序员:左正康 发表时间:2013/12/20 14:24 代号:黑眼圈的日子 第一步:导入dem ...