zoukankan      html  css  js  c++  java
  • POJ 2456 Aggressive cows

    Aggressive cows
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4808   Accepted: 2356

    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

     

    求解距离最小值可以取到的最大值(有点乱呃……就是最小值的最大化问题)

    求解的方法依然是二分搜索,和POJ 1064差不多

    首先读入所有的x[i](0<=i<=n-1)后进行从小到大的排序,即可确定解区间为1~x[n-1]

    然后在解区间内用二分搜索的方法找到最终的答案。

     

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 
     6 using namespace std;
     7 
     8 int n,c;
     9 int x[100010];
    10 
    11 int main()
    12 {
    13     scanf("%d %d",&n,&c);
    14     for(int i=0;i<n;i++)
    15         scanf("%d",&x[i]);
    16     sort(x,x+n);
    17     int l=1,r=x[n-1];
    18     while(r-l>1)
    19     {
    20         int mid=(l+r)/2,ans=0;
    21         for(int i=0;ans<c&&i<n;)
    22         {
    23             int t=i;
    24             while(x[++i]-x[t]<mid);
    25             ans++;
    26         }
    27         if(ans>=c)
    28             l=mid;
    29         else
    30             r=mid;
    31     }
    32     if(l!=r)
    33     {
    34         int ans=0;
    35         for(int i=0;ans<c&&i<n;)
    36         {
    37             int t=i;
    38             while(x[++i]-x[t]<r);
    39             ans++;
    40         }
    41         if(ans>=c)
    42             l=r;
    43     }
    44     printf("%d
    ",l);
    45 
    46     return 0;
    47 }
    [C++]
  • 相关阅读:
    SCM基础之SCM配置管理计划重要性
    SCM基础之合理设计配置库
    SCM英文术语
    中国歼20隐形战机首飞成功
    SCM基础之过程描述
    SCM基础之基线审核
    SCM基础之组织结构设计
    SCM基础之如何做到配置管理
    配置管理介绍
    软件配置管理的任务
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3300407.html
Copyright © 2011-2022 走看看