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. php 调用天气接口

    前几天没事的时候,浏览博客看到了一篇免费天气接口的文章,然后调用了一下文章中提到的接口,自己琢磨了半天,把数据处理了一下,虽然现在用不到,但是说不定以后会用,所以打算记录一下,毕竟这也算是自己第一次在 ...

  2. win10 家庭版修改hosts的权限

    https://jingyan.baidu.com/article/624e7459b194f134e8ba5a8e.html

  3. 测试那些事儿—selenium自动化实战之登录验证码处理

    登陆时经常出现验证码自动化测试如何处理呢? 一般有如下几种处理思路: 1.通过接口请求,拿到对应验证码信息 2.让开发配合把验证码改成万能验证码 3.注入cookies 如何通过注入cookies的方 ...

  4. 《从Lucene到Elasticsearch:全文检索实战》学习笔记一

    今天,我主要给大家讲一下信息检索概念. 信息检索: 互联网时代的飞速发展使人们进入了信息爆炸时代,据统计全球的互联网用户已达到30亿,在各个网站及移动app在每个分钟 产生的数据量是巨大的,从而导致数 ...

  5. js 关于定时器的知识点。

    Js的同步和异步 同步:代码从上到下执行. 异步:每个模块执行自己的,同时执行. js本身就是同步的,但是需要记住四个地方是异步. Js的异步   1.定时器  2.ajax   3事件的绑定  4. ...

  6. SSL&TLS渗透测试

    什么是TLS&SSL? 安全套接字层(SSL)和传输层安全(TLS)加密通过提供通信安全(传输加密)和为应用程序如网络.邮件.即时消息和某些虚拟私有网络(VPN)提供隐私的方式来确保互联网和网 ...

  7. spring中使用@PostConstruct和@PreConstruct注解

    1.@PostConstruct说明 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法.被@PostCo ...

  8. 创建用户自定义函数 SQL

    //创建用户自定义函数------标量函数 create function dbo.bmrs(@bmh as int) returns int as begin declare @bmrs int s ...

  9. 服务器tcp连接timewait过多优化及详细分析

    [背景说明] 在7层负载均衡上,查询网络状态发现timewait太多,于是开始准备优化事宜 整体的拓扑结构,前面是lvs做dr模式的4层负载均衡,后端使用(nginx.or haproxy)做7层负载 ...

  10. Android中的Sqlite中的onCreate方法和onUpgrade方法的执行时机

    1.今天在做数据库升级的时候,遇到一个问题,就是onCreate方法和onUpgrade方法的执行时机的问题,这个当时在操作的时候,没有弄清楚,很是迷糊,后来看了相关的博客由于转发受限所以copy了一 ...