Pku2456 Binary Search

Source: Internet
Author: User

Original question http://poj.org/problem? Id = 2456

Aggressive cows
Time limit:1000 ms   Memory limit:65536 K
Total submissions:6001   Accepted:2989

Description

Farmer John has built a new long barn, with N (2 <= n <= 100,000) stils. the stallare located along a straight line at positions X1 ,..., xn (0 <= xi <= 1,000,000,000 ).

His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. to prevent the cows from hurting each other, FJ want to assign the cows to the stils, such that the minimum distance between any two of them is as large as possible. what is the largest minimum distance?

Input

* Line 1: two space-separated integers: N and C

* Lines 2. n + 1: line I + 1 contains an integer stall location, Xi

Output

* Line 1: One INTEGER: the largest minimum distance

Sample Input

5 312849

Sample output

3

Hint

Output details:

FJ can put his 3 cows in the stallat positions 1, 4 and 8, resulting in a minimum distance of 3.

Huge input data, scanf is recommended.

Source

Usaco 2005 February gold
// Obtain the maximum distance between the nearest two cows. // method. Directly divide the distance to find the answer # include <stdio. h> # include <stdlib. h> # include <malloc. h> // # include <limlits. h> # include <ctype. h> # include <string. h> # include <string> # include <math. h> # include <algorithm> # include <iostream> # include <stack> # include <queue> # include <deque> # include <set> # include <vector> # include <map> using namespace STD; # define n Limit 5int X [N]; int N, C; bool OK (INT dis) {int I; int next; int last = 0; for (I = 1; I <C; I ++) {// make sure that all cows meet next = last + 1; while (next <n & X [next]-X [last] <dis) {// continuously expand next ++;} If (next> = N) {// because the number of cows is not full at this time, it indicates that it cannot satisfy all cows. Therefore, the distance is too large. Return false;} Last = next;} return true ;} int main () {int I; while (~ Scanf ("% d", & N, & C) {memset (x, 0, sizeof (x); for (I = 0; I <N; I ++) {scanf ("% d", & X [I]) ;}sort (x, x + n); int L = 0; int r = 1000000000; // possible distance // int mid = L + (R-l)/2; int mid; while (R-l> 1) {// continuously narrow the distance, until the distance between them is mid = (L + r)/2; If (OK (MID) = true) {// if the distance is met, it indicates that, you can also find L = mid;} else {r = 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.