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


  • 相关阅读:
    pandas之中文分词,词云,情感分析,语义分析3
    pandas之中文分词,词云,情感分析,语义分析2
    pandas之中文分词,词云,情感分析,语义分析1
    大数据学习之-the king of bigdata
    pandas之分组聚合(agg,apply)
    pandas之表的长宽转换
    Pandas数据规整之合并
    Pandas之数据规整
    Pandas数据规整
    IP协议那些事
  • 原文地址:https://www.cnblogs.com/csushl/p/9386638.html
Copyright © 2011-2022 走看看