zoukankan      html  css  js  c++  java
  • poj2456-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.


    题意:在坐标轴上选取N个坐标,然后从中随意选取C个点,求相邻两点最近距离的最大值。

    题解:就是最小值最大化;


    AC代码为:


    #include<iostream>
    #include<cstdio>
    #include<algorithm>


    using namespace std;


    int main()
    {
    int N,C,left,right,mid;

    cin>>N>>C;

    int a[100005];

    for(int i=0;i<N;i++)
    {
    cin>>a[i];
    }

    sort(a,a+N);

    left=1,right=a[N-1];

    while(right>left+1)
    {

    mid=(left+right)/2;

    int cns=0; 

    for(int i=0;i<N;)
    {
    int x=i;
    while(a[++i]-a[x]<mid);
    cns++;
    }
    if(cns>=C)
    {
    left=mid;
    }
    else
    {
    right=mid;
    }

    }

    if(left!=right)
    {
    int cns=0;

    for(int i=0;i<N;)
    {
    int t=i;

    while(a[++i]-a[t]<right);
    cns++;
    }

    if(cns>=C)
    left=right;
    }

    printf("%d ",left);

    return 0;
    }


  • 相关阅读:
    qt忙等与非忙等
    获得文件路径 _pgmptr, _makepath, _splitpath
    RGB2YCbCr RGB2Gray
    qt Cannot connect creator comm socket /tmp/qt_temp.S26613/stub-socket: No such
    64位Ubuntu系统安装OpenCV 2.4.x+ffmpeg 完美解决方案
    vim按下ctrl+s僵死
    win32程序应用mfc库
    error LNK2005: _DllMain@12 已经在 dllmain.obj 中定义
    JavaScript中的浅拷贝和深拷贝
    Set和Map
  • 原文地址:https://www.cnblogs.com/csushl/p/9386638.html
Copyright © 2011-2022 走看看