BZOJ1734: [Usaco2005 feb]aggressive cows angry cow binary search

Source: Internet
Author: User

Description

Farmer John had built a new long barn, with N (2 <= n <= 100,000) stalls. The stalls is 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 all other, FJ want to assign the cows to the stalls, such that the minimum distance bet Ween any and them is as large as possible. What's the largest minimum distance?

Farmer John built a long corral that consisted of NN (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 a few cows in a compartment, they will be fighting. In order not to let the cows hurt each other. John decided to allocate a compartment for the cows so that the minimum distance between any two cows is as large as possible, so what is the maximum minimum distance?

Input

* Line 1:two space-separated integers:n and C * Lines 2..n+1:line i+1 contains a integer stall location, xi

First line: space-delimited two integers n and c

The second line---line n+1: The i+1 line points to the position of Xi

Output

* Line 1:one integer:the largest minimum distance

First line: An integer, the maximum minimum value

Sample Input5 3
1
2
8
4
9

Sample Output3

Put the cows in 1,4,8 so the minimum distance is 3 .Hintsource

Gold

Exercises

Two-point answer, to judge

1#include <iostream>2#include <cstdio>3#include <algorithm>4 using namespacestd;5 Const intmaxn=100000+1;6 intA[MAXN];7 intMainintargcChar*argv[])8 {9     //freopen ("b.in", "R", stdin);Ten     //freopen ("B.out", "w", stdout); One     intn,m,i; Ascanf"%d%d",&n,&m); -      for(i=1; i<=n;i++) -scanf"%d",&a[i]); theSort (A +1, a+n+1); -     intL=0, r=A[n],mid; -      while(l<R) -     { +Mid= (L+R)/2; -         Long Longtot=0, ans=0; +          for(i=2; i<=n;i++) A         if(tot+a[i]-a[i-1]<=mid) tot=tot+a[i]-a[i-1]; at         Elsetot=0, ans++; -         if(ans<m-1) r=mid; -         ElseL=mid+1; -     } -printf"%d\n", L); -     return 0; in}

BZOJ1734: [Usaco2005 feb]aggressive cows angry cow binary search

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.