• poj 2456 Aggressive cows


    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

     
    给出n个stall的位置,要把c头牛安排进去,要去任意两头之间的距离最够大,就是说满足条件的方案应该是,任意两头牛之间的最少距离尽可能的大,也可以说是尽量分散的均匀一些。stall的位置是确定的,数据量很大,所以肯定不是选取位置,因为要求的是距离,可以从距离入手,要求最大的最小距离,即最小值最大化,可以二分确定最符合的标准,给定一个d是最小距离,从左往右开始安排牛,看看能不能在保证这个最小距离的前提下把所有的牛都安排了,如果能就是满足的,二分求得最大值。
    代码:
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    
    using namespace std;
    int s[100000],n,c;
    bool check(int d) {
        int last = s[0],cc = 1;
        for(int i = 1;i < n;i ++) {
            if(s[i] - last >= d) {
                cc ++;
                last = s[i];
            }
        }
        return cc >= c;
    }
    int main() {
        scanf("%d%d",&n,&c);
        for(int i = 0;i < n;i ++) {
            scanf("%d",&s[i]);
        }
        sort(s,s + n);
        int l = 1,r = s[n - 1] / c;
        while(l < r) {
            int mid = (l + r + 1) / 2;
            if(check(mid)) l = mid;
            else r = mid - 1;
        }
        printf("%d",l);
        return 0;
    }
  • 相关阅读:
    dom2级事件兼容性写法
    cookie js案例
    cookie讲解
    js高级总结
    鼠标拖拽时,选择文字问题
    正则的细节
    正则捕获的细节及replace分析
    正则的使用及replace细讲
    while循环的讲解
    acwing 189. 乳草的入侵 bfs
  • 原文地址:https://www.cnblogs.com/8023spz/p/10705847.html
Copyright © 2020-2023  润新知