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;
    }


  • 相关阅读:
    马的遍历 new
    锤子剪刀布 new
    npm 打包 new
    Linux 锁机制
    ubuntu virtualbox 下安装xp,识别usb
    ubuntu设置快捷键
    linux神奇的系统请求系统救命草
    linux 内核动态内存分配测试(纯属娱乐哈)
    C之绝妙(一道很NB的面试题)
    虚拟机virtualbox:Could not find an open hard disk with UUID {368441269e88468698582d1a0568f53c}.
  • 原文地址:https://www.cnblogs.com/csushl/p/9386638.html
Copyright © 2011-2022 走看看