POJ3579 Median

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

POJ Founder Monthly Contest – 2008.04.13, Lei Tao

正解:二分答案

解题报告:

  今天考试的T2,考场上面还想了很久XD

  因为是求处在中位数的差值,所以直接二分这个差值x,对于每个数查找整个数列有多少个比他大x以上的数有多少直接checke检查合法性。

 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 using namespace std;
14 typedef long long LL;
15 #define RG register
16 const int MAXN = 50011;
17 int n;
18 LL a[MAXN];
19 LL l,r,ans,ans2;
20 LL N,cnt,zhong;
21
22 inline int getint()
23 {
24        RG int w=0,q=0; char c=getchar();
25        while((c<‘0‘ || c>‘9‘) && c!=‘-‘) c=getchar(); if(c==‘-‘) q=1,c=getchar();
26        while (c>=‘0‘ && c<=‘9‘) w=w*10+c-‘0‘, c=getchar(); return q ? -w : w;
27 }
28
29 inline bool check(LL x){
30     cnt=0; LL now;
31     for(RG int i=1;i<=n;i++) {
32     now=lower_bound(a+i,a+n+1,a[i]+x)-a;
33     cnt+=(LL)n-now+1;
34     }
35     if(cnt>zhong) return true; return false;
36 }
37
38 inline void work(){
39     while(scanf("%d",&n)!=EOF) {
40     for(RG int i=1;i<=n;i++) a[i]=getint();
41     sort(a+1,a+n+1); l=0; r=a[n]-a[1]+1; a[n+1]=(1<<30);
42     N=(LL)n*(n-1)/2; LL mid;
43     zhong=N/2; ans=0;
44     while(l<=r) {
45         mid=(l+r)/2;
46         if(check(mid)) ans=mid,l=mid+1;
47         else r=mid-1;
48     }
49     printf("%lld\n",ans);
50     }
51 }
52
53 int main()
54 {
55   work();
56   return 0;
57 }
时间: 2024-11-05 13:56:33

POJ3579 Median的相关文章

poj3579 median 二分搜索 中位数

Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3866   Accepted: 1130 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) difference

POJ3579 Median —— 二分

题目链接:http://poj.org/problem?id=3579 Median 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 < 

POJ3579 Median(二分答案 + O(N)判定)

传送门 大意:给出N个数,对于存有每两个数的差值的序列求中位数,如果这个序列有偶数个元素,就取中间偏小的作为中位数. 因为N<=100000,所以想要求出每一个差值是不可行的,我们很容易想到二分答案. 在二分答案时我们会进行判定,求出小于等于枚举值的个数,我看其他人的判定似乎都是O(NlogN) 的,我在这里就给出一个O(N)的判定方法. 首先同样将数组排序(我们命名为a数组好了) 我们枚举一个区间[l,r),因为当r增加的时候,要使[l,r)中的数都大于于等于a[r]?枚举值,l必定不会增加,

【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 ≤

LeetCode OJ 4. Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

leetcode笔记:Find Median from Data Stream

一. 题目描述 Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 De

zoj Median (multiset)

Median Time Limit: 5 Seconds      Memory Limit: 65536 KB The median of m numbers is after sorting them in order, the middle one number of them ifm is even or the average number of the middle 2 numbers if m is odd. You have an empty number list at fir

4. Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

LeetCode-刷题 Median of Two Sorted Arrays

之前一直是写C++的,想着开始学学用JAVA写Code会有什么不同,是否会更加简洁?于是开始学着用JAVA去刷LEETCODE 习题如下: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example