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. python之django 资料

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

  2. Lintcode: Maximum Subarray Difference

    Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is ...

  3. CSS自定义弹出框

    <script type="text/javascript" language="javascript"> function sAlert(str) ...

  4. JavaScript: basis

    ref: http://www.imooc.com/code/387 1. html里直接嵌入js: <!DOCTYPE HTML> <html> <head> & ...

  5. 当android studio一直显示gradle compile dependency

    出现这种情况,是被墙的问题,我的解决办法是这样的: 打开file---->setting,然后搜索gradle,把offline勾上,然后点击apply以及ok,就可以了. 有时候它会关闭,只需 ...

  6. Java编程思想(一):大杂烩

    在java中一切都被视为对象.尽管一切都是对象,但是操纵的标识符实际上是对象的一个引用,可以将引用想象成是遥控器(引用)来操纵电视机(对象).没有电视机,遥控器也可以单独存在,即引用可以独立存在,并不 ...

  7. SparkSQL JSON数据操作(1.3->1.4)

    1.用户自定义schema data json串格式如下: { "partner_code": "demo", "app_name": &q ...

  8. 关于.Net Remoting 和 Web Servcie的比较

    参照文献 http://www.cnblogs.com/shinehouse/articles/3001955.html http://www.cnblogs.com/paper/archive/20 ...

  9. Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别

    Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别 时间 2012-12-19 20:30:12  CSDN博客原文  http://blog.cs ...

  10. 夺命雷公狗—angularjs—6—单条数据的遍历

    我们在实际的工作中常常会处理到一些数据的遍历,这些数据都是后端传到前端的,有些公司会让前端帮忙处理一点遍历的工作,废话不多说,直接上代: <!doctype html> <html ...