Poj3579--median

Source: Internet
Author: User

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;
}


Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.