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

    • 原题如下:
      Aggressive cows
      Time Limit: 1000MS   Memory Limit: 65536K
      Total Submissions: 20524   Accepted: 9740

      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

      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.
    • 题解:类似的最大化最小值或者最小化最大值的问题,通常用二分搜索法解决。
      我们定义:C(d):=可以安排牛的位置使得最近的两头牛的距离不小于d,那么问题就变成了求满足C(d)的最大的d。另外,最近的间距不小于d也可以说成是所有牛的间距都不小于d,因此就有C(d)=可以安排牛的位置使得任意的牛的间距都不小于d,这个问题的判断使用贪心法就可以解决。
    • 代码:
       1 #include <cstdio>
       2 #include <cctype>
       3 #include <algorithm>
       4 #include <cmath>
       5 #define num s-'0'
       6 
       7 using namespace std;
       8 
       9 const int MAX_N=101000;
      10 const int INF=0x3f3f3f3f;
      11 int N,M;
      12 int x[MAX_N];
      13 
      14 void read(int &x){
      15     char s;
      16     x=0;
      17     bool flag=0;
      18     while(!isdigit(s=getchar()))
      19         (s=='-')&&(flag=true);
      20     for(x=num;isdigit(s=getchar());x=x*10+num);
      21     (flag)&&(x=-x);
      22 }
      23 
      24 void write(int x)
      25 {
      26     if(x<0)
      27     {
      28         putchar('-');
      29         x=-x;
      30     }
      31     if(x>9)
      32         write(x/10);
      33     putchar(x%10+'0');
      34 }
      35 
      36 bool C(int);
      37 
      38 int main()
      39 {
      40     read(N);read(M);
      41     for (int i=0; i<N; i++) read(x[i]);
      42     sort(x, x+N);
      43     int lb=0, ub=INF;
      44     while (ub-lb>1)
      45     {
      46         int mid=(lb+ub)/2;
      47         if (C(mid)) lb=mid;
      48         else ub=mid;
      49     }
      50     write(lb);
      51     putchar('
      ');
      52 }
      53 
      54 bool C(int d)
      55 {
      56     int last=0;
      57     for (int i=1; i<M; i++)
      58     {
      59         int crt=last+1;
      60         while (crt<N && x[crt]-x[last]<d) crt++;
      61         if (crt==N) return false;
      62         last=crt;
      63     }
      64     return true;
      65 }
  • 相关阅读:
    十八、分页查询
    十七、oracle的权限和角色
    十六、同义词(synonyms)
    十五、序列
    十四、禁用与启动约束
    十三、修改表时添加约束
    十二、约束
    十一、修改表格
    十、事务(Transaction)
    Windows 远程连接后,自动断开,所有程序都自动关闭(待验证,待更新)
  • 原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9496344.html
Copyright © 2011-2022 走看看