The main idea: give a series, and then calculate the value of the difference between the numbers in the series, form a new series, the median number of novelty series
Analysis: After sorting the original sequence, first binary search here, find the median mid, and then determine if the mid is large. Judging according to the following, the calculation of the original series after I, how many more than the number of x[i]+mid (the value of the original sequence is larger than X[i]+mid, indicating that the value and X[i] to form a new sequence in the number than the median, that is, mid-large), here to find the number of the second second two points search, You can use Lower_bound directly. If the sum of the numbers is less than half the number of new series, then the mid is large.
Code:
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 1111111;
int n;
int X[MAXN];
Long long m;
BOOL C (int mid) {
long long cnt = 0;
for (int i = 0; i < n; i++)
cnt + = X+n-lower_bound (x+i+1, X+n, x[i]+mid);
return CNT <= M/2; The equals sign is because CNT calculates a number larger than mid
}
int main () {while
(~scanf ("%d", &n)) {for
(int i = 0; i < n; i++)
SCANF ("%d", &x[i]);
Sort (x, x+n);
m = N (n-1)/2; The number of new series is c_n_2 (combination number), N is the number of the original series
int L = 0, R = *max_element (x, x+n);
while (R-l > 1) {
int mid = (l+r)/2;
if (C (mid)) R = mid;
else L = mid;
}
printf ("%d\n", L);
}
return 0;
}