zoukankan      html  css  js  c++  java
  • POJ 2456 Aggressive cows(二分答案)

    Aggressive cows
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 22674   Accepted: 10636

    Description

    Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are 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 stalls, 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 3
    1
    2
    8
    4
    9

    Sample Output

    3

    Hint

    OUTPUT DETAILS: 

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

    Huge input data,scanf is recommended.

    Source

     

    【题意】

    农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000,000,000). 但是,John的C (2 <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗。为了不让牛互相伤害。John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢

     

    【分析】

    贪心+二分
    二分枚举相邻两牛的间距,判断大于等于此间距下能否放进所有的牛。 

     

    【代码】

    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    #define debug(x) cerr<<#x<<" "<<x<<'
    ';
    using namespace std;
    inline int read(){
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    const int N=1e5+5;
    int ans,n,m,d[N];
    inline bool check(int now){
    	int res=1,p=d[1];
    	for(int i=2;i<=n;i++)
    		if(d[i]-p>=now)
    			res++,p=d[i];
    	return res>=m;
    }
    int main(){
    	n=read(),m=read();
    	for(int i=1;i<=n;i++) d[i]=read();
    	sort(d+1,d+n+1);
    	int l=0,r=d[n],mid;
    	while(l<=r){
    		mid=l+r>>1;
    		if(check(mid)){
    			ans=mid;
    			l=mid+1;
    		}
    		else{
    			r=mid-1;
    		}
    	}
    	printf("%d
    ",ans);
    	return 0;
    }
     


     

     

     

  • 相关阅读:
    前五章的综合(查漏补缺)一个字一个字读,便于您的理解程度
    appium连接android真机【红米note8】碰到的问题及解决方案
    linux tz配置
    linux下卸载apache
    VM下的静态ip和动态ip的坑
    linux下安装mysql
    LR录制过程中会出现的问题
    linux下安装jdk和tomcat
    jmeter的环境变量
    jmeter实现mysql的增删改查
  • 原文地址:https://www.cnblogs.com/shenben/p/10311950.html
Copyright © 2011-2022 走看看