zoukankan      html  css  js  c++  java
  • bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案

    [Usaco2005 feb]Aggressive cows 愤怒的牛

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 407  Solved: 325
    [Submit][Status][Discuss]

    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?

    农夫 John 建造了一座很长的畜栏,它包括NN (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000,000,000). 但是,John的C (2 <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗。为了不让牛互相伤害。John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢

    Input

    * Line 1: Two space-separated integers: N and C * Lines 2..N+1: Line i+1 contains an integer stall location, xi

    第一行:空格分隔的两个整数N和C

    第二行---第N+1行:i+1行指出了xi的位置

    Output

    * Line 1: One integer: the largest minimum distance

    第一行:一个整数,最大的最小值

    Sample Input

    5 3
    1
    2
    8
    4
    9

    Sample Output

    3

    把牛放在1,4,8这样最小距离是3

    HINT

     

    Source

     
    题解:
      题意是给定数轴上n个点,要取出其中m个点使得相邻的点的最小距离最大。
      所以二分答案判断即可。
     1 #include<cstring>
     2 #include<cmath>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<cstdio>
     6 
     7 #define N 200007
     8 using namespace std;
     9 inline int read()
    10 {
    11     int x=0,f=1;char ch=getchar();
    12     while(ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    13     while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    14     return x*f;
    15 }
    16 
    17 int n,m,ans,l,r;  
    18 int a[N];  
    19 inline bool jud(int x)  
    20 {  
    21     int tot=1,now=1,save=1;  
    22     while (tot<=m)  
    23     {  
    24         while (a[now]-a[save]<x && now<=n) now++;  
    25         if (now>n && tot<m) return 0;  
    26         tot++;save=now;  
    27         if (tot==m) return 1;  
    28     }  
    29     return 1;  
    30 }  
    31 int main()  
    32 {  
    33     n=read();  
    34     m=read();  
    35     for (int i=1;i<=n;i++) a[i]=read();  
    36     sort(a+1,a+n+1);  
    37     l=1;r=1000000000;  
    38     while (l<=r)  
    39     {  
    40         int mid=(l+r)>>1;  
    41         if (jud(mid)){ans=mid;l=mid+1;}  
    42         else r=mid-1;  
    43     }  
    44     printf("%d",ans);  
    45 }  
  • 相关阅读:
    生活小记--工作一年后的菜鸡
    git使用笔记-git项目的建立及配置、创建子分支及独立分支、分支重命名
    React-leaflet在ant-design pro中的基本使用
    ionic新入坑-环境搭建+新建项目+打开低版本项目处理
    canvas绘制圆心扇形可组成颜色随机的七色小花
    取所选当前时间前十二个月的数据
    win10被微软流氓更新后编译基于visual Studio的web项目报[ArgumentOutOfRangeException: 指定的参数已超出有效值的范围
    浅析__proto__、prototype
    JavaScript数据类型
    异步与多线程实现不阻塞区别
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8184475.html
Copyright © 2011-2022 走看看