Poj 2456 aggressive cows, two points, maximizing the minimum value

Source: Internet
Author: User
Description
Farmer John built a long barrier, which contains N (2 <= n <= 100,000) compartments numbered X1 ,..., xn (0 <= xi <= 1,000,000,000 ).
However, John's C (2 <= C <= N) cows do not like this layout, and when a few cows are placed in a compartment, they are about to fight. In order not to let the ox hurt each other. John decided to allocate his own compartment for the ox so that the minimum distance between any two cows is as large as possible. What is the maximum and minimum distance?
Input
There are multiple groups of test data, ending with EOF.
First line: two integers N and C separated by Spaces
The second line -- line n + 1 indicates the position of Xi respectively.
Output
Each group of test data outputs an integer that satisfies the maximum and minimum values of the question. Note the line feed.
Sample Input
5 3
1
2
8
4
9
Sample output

3


Minimum binary distance after sorting

#include<cstdio>#include<algorithm>using namespace std;const int maxn = 100000 + 10;int x[maxn];int n, c;int ok(int k) {    int last = 0;    int cur;    for(int i=1; i<c; ++i) {        cur = last + 1;        while(cur<n && x[cur] - x[last] < k)            cur++;        if(cur==n) return false;        last = cur;    }    return true;}int main() {    scanf("%d%d",&n, &c);    for(int i=0; i<n; ++i) scanf("%d", &x[i]);    sort(x, x+n);    int l = 0, r = (x[n-1] - x[0])/(c-1);    for(int i=0; i<100; ++i) {        int mid = (l+r)/ 2;        if( !ok(mid) )  r = mid;        else l = mid;    }    printf("%d\n", l);    return 0;}



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.