zoukankan      html  css  js  c++  java
  • 二分 例题2

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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.
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <math.h>
     4 #include <algorithm>
     5 using namespace std;
     6 const int INF=1e9+7;
     7 
     8 int N,m;
     9 int a[100005];
    10 
    11 bool C(int d)
    12 {
    13     int last=1;
    14     for(int i=1;i<m;i++)
    15     {
    16         int crt=last+1;
    17         while(crt<=N && a[crt]-a[last]<d)
    18         {
    19             crt++;
    20         }
    21         if(crt==N+1)
    22             return false;
    23         last=crt;
    24     }
    25     return true;
    26 }
    27 
    28 int main()
    29 {
    30     int i,j;
    31     while(scanf("%d %d",&N,&m)!=EOF)
    32     {
    33         for(i=1;i<=N;i++)
    34             scanf("%d",&a[i]);
    35         sort(a+1,a+N+1);
    36         int lb=0,ub=INF;
    37         while(ub-lb>1)
    38         {
    39             int mid=(lb+ub)/2;
    40             if(C(mid))
    41                 lb=mid;
    42             else
    43                 ub=mid;
    44         }
    45         printf("%d
    ",lb);
    46     }
    47     return 0;
    48 }
    View Code
  • 相关阅读:
    js日期加减得到新的日期的自定义函数
    c# winform 一个可以用鼠标改变控件位置和大小的类,调用即可
    颜色英文代码全集
    你的Web系统有多少安全漏洞
    robots协议和禁止搜索引擎收录
    Linux下./configure错误详解
    c# winform 关于DataGridView的一些操作
    通过JSONP实现完美跨域
    tabs选项卡
    jQuery 1.4的十五大新功能实例精讲
  • 原文地址:https://www.cnblogs.com/cyd308/p/4693662.html
Copyright © 2011-2022 走看看