zoukankan      html  css  js  c++  java
  • POJ

    Aggressive cows
    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 18099 Accepted: 8619

    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

    题目链接:点击打开链接
    题目大意:给牛分配隔间,使任意两头牛之间的最小距离尽可能的大
    思路:先将牛的位置按从小到大的顺序排序,再二分枚举查找,左值为0,右值为最左端与最右端的牛的距离。
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    
    int n, c,x[100005];
    
    int check(int mid)
    {
    	int l = 0;
    	for (int i = 1; i < c; i++)
    	{
    		int r = l + 1;
    		while (r < n&&x[r] - x[l] < mid)
    			r++;
    		if (r == n)return 0;
    		l = r;
    	}
    	return 1;
    }
    
    
    int main()
    {
    	while (~scanf("%d %d", &n, &c))
    	{
    		for (int i = 0; i < n; i++)
    			scanf("%d", &x[i]);
    		sort(x, x + n);
    		int left = 0, right = x[n-1];
    		while (right > left +1)
    		{
    			int mid = (right + left) / 2;
    			if (check(mid))
    				left = mid;
    			else
    				right = mid;
    		}
    		printf("%d
    ", left);
    	}
    
    	return 0;
    }

  • 相关阅读:
    shell中单引号、双引号、反斜杠简说
    shell脚本
    求素数
    SqlBulkCopy高效写入数据库Demo
    地图面面观之百望山
    FileUpload控件客户端验证
    如何将shapefile进行拆分
    Python 字符串操作
    如何重装oracle
    资料
  • 原文地址:https://www.cnblogs.com/csu-lmw/p/9124463.html
Copyright © 2011-2022 走看看