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++]
  • 相关阅读:
    CRL快速开发框架系列教程二(基于Lambda表达式查询)
    CRL快速开发框架系列教程一(Code First数据表不需再关心)
    非关系型数据库来了,CRL快速开发框架升级到版本4
    CRL快速开发框架开源完全转到Github
    火车订票系统的高并发解决方案
    用wordpress搭建个人博客
    centos/redhat安装mysql
    vue之nextTick全面解析
    微信公众号开发笔记3-sdk接入(nodejs)
    微信公众号开发笔记2(nodejs)
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3300407.html
Copyright © 2011-2022 走看看