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
翻译:有n间牛房在一条直线上,距离分别是xi,有c只牛,牛在一起容易打架,让牛尽可能在距离比较远的牛房。要安置c只牛,求这个距离最大能是多少?
白书上的二分题目,书上讲得一点都不人性化,能用人话说清楚的非要用鬼话,代码也是不够简洁,看了半个小时都不懂,一搜题解,秒懂,记录一下代码。
 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int n,m;
int a[]; bool check(int d)
{
int num=,now=a[];
for(int i=;i<n;i++)
{
if(a[i]-now>=d)
now=a[i],num++;///如果a[i]号牛棚距离上一个的距离>=d,更新牛棚位置,并且增加安置的牛数,大于等于m时候就是可行解
if(num>=m)
return true;
}
return false;
} int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int l=,r=a[n-],ans=;
while(l<=r)
{
int mid=(l+r)/;
if(check(mid))
{
ans=mid;///不断更新可行解,找到最大值, 即最大化 可行解。
l=mid+;
}
else
r=mid-;
}
printf("%d\n",ans);
}
return ;
}
 

POJ2456Aggressive cows-(二分判定)的更多相关文章

  1. 二分判定 覆盖问题 BZOJ 1052

    //二分判定 覆盖问题 BZOJ 1052 // 首先确定一个最小矩阵包围所有点,则最优正方形的一个角一定与矩形一个角重合. // 然后枚举每个角,再解决子问题 #include <bits/s ...

  2. 【二分查找】POJ2456-Aggressive cows

    [题目大意] 有N间牛舍和M头牛,告诉你每个牛舍的位置,求出两头牛之间最小距离的最大值. [思路] 二分判断两头牛之间的最小距离d,通过贪心法进行验证. #include<iostream> ...

  3. 【BZOJ-2440】完全平方数 容斥原理 + 线性筛莫比乌斯反演函数 + 二分判定

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2371  Solved: 1143[Submit][Sta ...

  4. POJ2456 Aggressive cows 二分

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...

  5. POJ 2456 Agressive cows(二分)

    POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...

  6. POJ 2456 Aggressive cows(二分答案)

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...

  7. POJ - 2456 Aggressive cows 二分 最大化最小值

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18099   Accepted: 8619 ...

  8. [poj 2456] Aggressive cows 二分

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...

  9. [USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

随机推荐

  1. Ajax之Json对象序列化传参

    前端部分:传入参数 "groupObject" : jsonString //对象序列化传参 var projectGroup = {}; projectGroup["i ...

  2. 3.4 unittest之装饰器(@classmethod)

    3.4 unittest之装饰器(@classmethod) 前言前面讲到unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例 ...

  3. Zookeeper之入门(原理、基础知识)

    Zookeeper介绍 Zookeeper是分布式应用程序的协调服务框架,是Hadoop的重要组件.ZK要解决的问题: 1.分布式环境下的数据一致性. 2.分布式环境下的统一命名服务 3.分布式环境下 ...

  4. day03字符串

    字符串 1.方法详细内容 方法 强制转换 ##### 1.大小写转换 Value.upper() / value.lower() new_value=value.upper() print(new_v ...

  5. Linux源码安装软件

    Linux环境下 源码编译安装软件 ==== 1. 下载,步骤略 ==== 2. 验证,部分官方下载同时提供签名,MD5,sha1,sha256等校验信息. $ gpg --verify gnupg- ...

  6. CSS样式补充第二天

    #p1{/*        border-width: 1px;*/        /*边框实线*/        /*border-style: solid;*/        /*边框虚线*/   ...

  7. Java面向对象 第4节 类的多态性

    一.多态的概念 在面向对象语言中,多态是指在一棵继承树中的类中可以有多个同名但不同方法体及不同形参的方法.通常有两种途径实现多态:方法的重载和覆盖. 多态性允许以统一的风格处理已存在的变量及相关的类. ...

  8. ios-微信支付登录分享-notification通知

    // //  AppDelegate.m //  NewAppBase // //  Created by ENERGY on 2018/5/17. //  Copyright © 2018年 ENE ...

  9. 如何在Windows命令行(DOS界面)中调用 编译器 来编译C/C++源程序

    首先说明一下背景: 为什么要在DOS界面编译C/C++源程序?有很多现成的开发环境(IDE)如:vs, vc++等,这些开发环境集成了编译,调试,使用起来很方便,而且图形化操作界面,简洁明了.但是在开 ...

  10. 【剑指offer】求一组数据中最小的K个数

    题目:输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. *知识点:Java PriorityQueue 调整新插入元素 转自h ...