Median
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 4379 |
|
Accepted: 1340 |
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 are defined as the (M/2)-th smallest number if m,the amount of the differences, is even. For example, you had 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'll be given on the first line. Then N numbers is given, representing X1, X2, ..., XN, (xi≤1,000,000,000 3≤n≤1,00,000)
Output
For each test case, the output of the median in a separate line.
Sample Input
4
1 3 2 4
3
1 10 2
Sample Output
1
8
Classic two-Part two-point topic each time the two-a[i]+x position to determine how much bigger than it then determine if it is not the median
The AC code is as follows:
Created by Taosama on 2015-04-28//Copyright (c) Taosama.
All rights reserved. #include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include < cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include &
lt;queue> #include <string> #include <set> #include <vector> using namespace std;
const int INF = 0X3F3F3F3F;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int n, a[n];
Long long half;
BOOL Check (int x) {int cnt = 0;
for (int i = 1; I <= n; ++i) cnt + A + n-lower_bound (A + 1, a + 1 + N, A[i] + x) + 1;
return cnt > half; } int main () {#ifdef LOCAL freopen ("In.txt", "R", stdin);//Freopen ("OUT.txt", "w", stdout); #endif Ios_base::sy
Nc_with_stdio (0);
while (scanf ("%d", &n) = = 1) {for (int i = 1; I <= n; ++i) scanf ("%d", A + i);
Sort (A + 1, a + 1 + N);
Half = n * (n-1ll) >> 2; int L = 0, r = 1e9;
while (L + 1 < r) {int mid = L + R >> 1;
if (check (mid)) L = mid;
else R = Mid;
} printf ("%d\n", L);
} return 0;
}