P1824 attacking cows, P1824 attacking cows

Source: Internet
Author: User

P1824 attacking cows, P1824 attacking cows
Description

Farmer John built a cowshed with N (2 <= N <= 100,000) compartments distributed in a straight line in the coordinates of x1 ,..., xN (0 <= xi <= 1,000,000,000 ).

His C (2 <= C <= N) cows are less than the compartment distribution, and they are angry with the presence of other cows in the cowshed. Farmer John wants to place the cows in a specified compartment to prevent them from fighting each other. The closer they are to each other, the better. So what is the maximum recent distance?

Input/Output Format

Input Format:

 

Row 3: two numbers N and C separated by spaces.

2nd ~ N + 1 rows: each row has an integer representing the coordinates of each compartment.

 

Output Format:

 

The output contains only one row, that is, the maximum closest distance between two adjacent cows.

 

Input and Output sample input sample #1:
5 31 2 8 4 9  
Output sample #1:
3

 

 

Bipartite answer + Partition Method

 

 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 const int MAXN=100001; 5 inline void read(int &n) 6 { 7     char c=getchar();bool flag=0;n=0; 8     while(c<'0'||c>'9') c=='-'?flag==1,c=getchar():c=getchar(); 9     while(c>='0'&&c<='9')    n=n*10+c-48,c=getchar();10 }11 int n,c;12 int a[MAXN];13 int pd(int val)14 {15     int now=a[1],tot=c-1;16     for(int i=2;i<=n;i++)17         if(a[i]-now>=val)18             now=a[i],tot--;19     if(tot<=0)    return 1;20     else         return 0;21 }22 int main()23 {24     read(n);read(c);25     for(int i=1;i<=n;i++)    read(a[i]);26     sort(a+1,a+n+1);27     int l=0,r=a[n];28     int ans;29     while(l<=r)30     {31         int mid=l+r>>1;32         if(pd(mid))    33             l=mid+1,ans=mid;34         else         35             r=mid-1;36     }37     printf("%d",ans);38     return 0;39 }

 

 

Related Article

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.