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.

 
要把题目读懂!
 
二分枚举
代码:
#include <iostream>
#include <algorithm> using namespace std;
const long long INF = 1000000002;
int a[100002], C, n; bool OK(int m)
{
int last = 0;
for(int i=1; i<C; i++)
{
int cur = last + 1 ;
while(cur<n && a[cur]-a[last]<m) cur ++ ;
if(cur==n) return false ;
last = cur ;
}
return true;
} int main()
{
cin >> n >> C ;
for(int i=0; i<n; i++) cin >> a[i] ;
sort(a,a+n);//排序
int low = 0, high = a[n-1] + 1 ;
while(high - low > 1)
{
int mid = (low + high) >> 1 ;
if(OK(mid)) low = mid ;
else high = mid ;
}
cout << low << endl ; return 0;
}

ACM题目————Aggressive cows的更多相关文章

  1. [ACM] poj 2456 Aggressive cows (二分查找)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 D ...

  2. poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分

    poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...

  3. POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13993   Accepted: 6775 ...

  4. 二分算法的应用——最大化最小值 POJ2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...

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

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

  6. POJ 2456 Aggressive cows (二分 基础)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7924   Accepted: 3959 D ...

  7. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

  8. 【POJ - 2456】Aggressive cows(二分)

    Aggressive cows 直接上中文了 Descriptions 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x ...

  9. 二分-F - Aggressive cows

    F - Aggressive cows Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. ...

随机推荐

  1. ECharts 在同一个页面添加多个图表 并 给图表绑定事件

      <!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts< ...

  2. linux 硬件信息

    1. 查看物理CPU的个数 #cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l 2. 查看逻辑CPU的个数 #cat / ...

  3. 理解JDBC和JNDI

    下面的英文是我找过来的,因为是英文所以不敢翻译出来误导别人,但是它描述的确实恰到好处,比所谓网上的JNDI和JDBC云云的解释要精辟很多,如果遇到不认识的单词,用有道吧~~:) The Java Na ...

  4. python之django 资料

    里边有不少比较好的文章. http://www.cnblogs.com/luxiaojun/p/5795070.html

  5. weiphp---------图灵机器人存在的bug。

    1.很多人下载下来weiphp源码以后,配置好了图灵机器人却不能使用.原因是因为他源码里面存在一个小bug 上图红色框框内是他的源码,问题就出在这里. 修改方法: if($result ['code' ...

  6. Lucas

    C(n,m)%p=C(n%p,m%p)*C(n/p,m/p)%p 迭代递归 n,m非负整数,p质数 证明 最后一个由二项式定理和p进制数性质得出的我并没有看懂...

  7. clock gating and PLL

    一个gating的clock是指:clock network除了包含inverter和buffer外,还有其他logic. PrimeTime会自动的对gating input进行setup和hold ...

  8. webpack我遇到的一些坑

    我的第一个用于实验webpack的项目是一个拥有多个版本的项目.什么叫多个版本?这个项目对3个语言版本+3个不同城市版本+(移动端  + PC端),也就是3*3*2,18个版本. 我的第一次想法肯定是 ...

  9. 解决windows的控制台显示utf8乱码的问题

    在控制台的属性里 修改自体为: 新宋体 在控制台下执行命令: chcp 65001

  10. 使用 nano 的时候提示找不到 libncursesw.so.5 这个共享库

    由于拿nano来练习pacman这个包管理系统,也就是卸了装,装了卸.但是后来当我想用 nano 编辑文件的时候,系统提示标题上说的这错误. 不知道做了什么,难道是误删了什么东西...后来几番搜索,用 ...