POJ 3579
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3528 | Accepted: 1001 |
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 <algorithm> using namespace std; #define maxn 100005
#define INF 200005
typedef long long ll; int n;
int a[maxn],dis[maxn];
ll num; bool judge(int x) { int pos,i = ,now = ;
ll sum = ;
while(i < n) {
pos = upper_bound(dis + ,dis + n + ,x + now) - dis;
sum += n - (pos);
now = dis[i];
if((n - ) - pos + == ) break;
++i;
} //printf("x = %d sum = %lld\n",x,sum); return num - sum - (num % ) >= sum;
} void solve() { num = (ll)n * (n - ) / ; int l = INF,r = a[n] - a[];
for(int i = ; i < n; ++i) {
dis[i] = a[i + ] - a[];
l = min(l,a[i + ] - a[i]);
// printf("%d ",dis[i]);
}
dis[n] = INF; //printf("l = %d r = %d\n",l,r); while(l < r) {
int mid = (l + r) >> ;
if(judge(mid)) {
r = mid;
} else {
l = mid + ;
}
} printf("%d\n",l); } int main()
{
// freopen("sw.in","r",stdin); while(~scanf("%d",&n)) {
for(int i = ; i <= n; ++i) {
scanf("%d",&a[i]);
} sort(a + ,a + n + ); solve();
} return ;
}
POJ 3579的更多相关文章
- POJ 3579 3685(二分-查找第k大的值)
POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...
- POJ 3579 Median(二分答案+Two pointers)
[题目链接] http://poj.org/problem?id=3579 [题目大意] 给出一个数列,求两两差值绝对值的中位数. [题解] 因为如果直接计算中位数的话,数量过于庞大,难以有效计算, ...
- poj 3579 Median (二分搜索之查找第k大的值)
Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...
- POJ 3579 Median(二分答案)
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11599 Accepted: 4112 Description G ...
- POJ 3579 Median 二分加判断
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12453 Accepted: 4357 Descripti ...
- POJ 3579 二分
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7687 Accepted: 2637 Descriptio ...
- POJ 3579 Median (二分)
...
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- Divide and conquer:Median(POJ 3579)
快速求两数距离的中值 题目大意:给你一个很大的数组,要你求两个数之间的距离的中值 二分法常规题,一个pos位就搞定的事情 #include <iostream> #include ...
随机推荐
- LLVM language 参考手册(译)(6)
模块级内联汇编(Module-Level Inline Assembly) 模块包含“module-level inline assembly”块,这与GCC中的“file scope inline ...
- DWZ前端框架使用问题记录
心得体验:DWZ依赖特定的HTML结构,所以一定要注意项目中的HTML结构,多用firebug查看,还有如果使用一些组件的时候出现问题,可以查看下返回JSON格式是否符合组件规定的JSON格式,很多都 ...
- WebStorm 快捷键收藏
快捷键 Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/-/ ) Ctrl+X 或 Ctrl+Y 删除一行 Shift+F6 重构-重命名 Alt+~vcs操作 Alt+~ 7关闭重 ...
- SOAP Web 服务介绍
已经跨入 2015 年,当今最为常见的两种 Web 服务类型分别是: REST 和 SOAP .不过,从趋势来看,越来越多人已经开始使用 REST 风格的 Web 服务.而 SOAP 大多也开始或已经 ...
- DataSnap数据库连接池,数据集对象池的应用
传统的应用服务器的开发往往是在ServerMethods单元中拖放一堆TDataSet, TDaTaSetProvider控件,这是一个最简单粗暴的开发方向,往往会造成服务端程序文件的臃肿.服务运行期 ...
- django - transaction
def user_atomic(): User.objects.create(name='purk1', email='pwu1@maxprocessing.com') User.objects.cr ...
- Python脚本控制的WebDriver 常用操作 <十七> 获取测试对象的属性及内容
测试用例场景 获取测试对象的内容是前端自动化测试里一定会使用到的技术.比如我们要判断页面上是否显示了一个提示,那么我们就需要找到这个提示对象,然后获取其中的文字,再跟我们的预期进行比较.在webdri ...
- linux c 验证登录密码
#define _XOPEN_SOURCE #include <stdio.h> #include <unistd.h> int main(int argc, char *ar ...
- ApacheBench 使用教程
ab压力测试 ab是Apache超文本传输协议(HTTP)的性能测试工具.其设计意图是描绘当前所安装的Apache的执行性能. 主要是显示你安装的Apache每秒可以处理多少个请求. 格式: ./ab ...
- php微型mvc框架创建步骤
创建数据库和表结构,并且添加模拟数据: 新建models.views.controllers.utilities和include五个文件夹:models:模型层 model.php:模型基类 ...