zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Bzoj 2134: [国家集训队2011]单选错位(期望)
    洛谷 P1230 智力大冲浪
    洛谷 P2695 骑士的工作
    洛谷 P1551 亲戚
    洛谷 P1111 修复公路
    洛谷 P1599 结算日
    HDU 1166 敌兵布阵
    洛谷 P2212 [USACO14MAR]浇地Watering the Fields
    洛谷 P1506 拯救oibh总部
    洛谷 P1396 营救
  • 原文地址:https://www.cnblogs.com/8023spz/p/10705847.html
Copyright © 2011-2022 走看看