• POJ 2456 Aggressive cows (二分)


    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.

    题意:

    FJ搭建了一间有N间牛舍的小屋。牛舍在一排直线上,第i号牛舍在xi的位置上。每头牛尽量放在尽可能远的牛舍,求最大化最近的两头牛之间的距离。

    题解:

    二分搞一下就好了。

    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int INF=1e9+5,maxn=1e5+5;
    int n,k;
    int a[maxn];
    bool check(int x)
    {
    	int cur=0,cnt=1;
    	for(int i=1;i<n;i++)
    	{
    		if(a[i]-a[cur]>=x)
    			cur=i,cnt++;
    		if(cnt>=k)
    			break;
    	}
    	return cnt>=k;
    }
    void solve()
    {
    	int lb=0,ub=INF;
    	while(lb<=ub)
    	{
    		int mid=(lb+ub)>>1;
    		if(check(mid))
    			lb=mid+1;
    		else
    			ub=mid-1;
    	}
    	cout<<ub<<endl;
    }
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin>>n>>k;
    	for(int i=0;i<n;i++)
    		cin>>a[i];
    	sort(a,a+n);
    	solve();
    	return 0;
    }
  • 相关阅读:
    剑桥雅思写作高分范文ESSAY59
    剑桥雅思写作高分范文ESSAY58
    剑桥雅思写作高分范文ESSAY57
    剑桥雅思写作高分范文ESSAY55
    剑桥雅思写作高分范文ESSAY54
    剑桥雅思写作高分范文ESSAY53
    剑桥雅思写作高分范文ESSAY52
    剑桥雅思写作高分范文ESSAY51
    博客园博客添加鼠标特效
    学会配置nginx
  • 原文地址:https://www.cnblogs.com/orion7/p/7682653.html
Copyright © 2020-2023  润新知