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. NET4.5之初识async与await

    这是两个关键字,用于异步编程.我们传统的异步编程方式一般是Thread.ThreadPool.BeginXXX.EndXXX等等.把调用.回调分开来,代码的逻辑是有跳跃的,于是会导致思路不是很清晰的问 ...

  2. log4cxx安装和使用

    log4cxx是Java社区著名的log4j的c++移植版,用于为C++程序提供日志功能,以便开发者对目标程序进行调试和审计,log4cxx是apache软件基金会的开源项目,基于APR实现跨平台支持 ...

  3. 在xocde运行profile 遇到"Existing default base temp directory '/Library/Caches/com.apple.dt.instruments' has insufficient privileges for user id 505. Please have the owner delete this directory"

    找到这篇文章 http://stackoverflow.com/questions/34153795/xcode-7-unable-to-open-instruments-from-developer ...

  4. Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  5. jsp页面指令

    JSP中共有三个指令: (1)page: 用于定义JSP文件中的全局属性 (2)include: 用于在JSP页面中包含另外一个文件的内容 (3)taglib: 此指令能够让用户自定义新的标签 第三个 ...

  6. 对while((pid = waitpid(-1, &stat, WNOHANG)) > 0)不懂的地方,现在懂了

    while((pid = waitpid(-1, &stat, WNOHANG)) > 0) 需要写到信号处理函数中,假如有10个子进程 只要父进程能够收到最后一个信号,就能把前面丢失的 ...

  7. CCF真题之最优灌溉

    201412-4 问题描述 雷雷承包了很多片麦田,为了灌溉这些麦田,雷雷在第一个麦田挖了一口很深的水井,所有的麦田都从这口井来引水灌溉. 为了灌溉,雷雷需要建立一些水渠,以连接水井和麦田,雷雷也可以利 ...

  8. paper 7:支持向量机系列四:Outliers —— 介绍支持向量机使用松弛变量处理 outliers 方法。

    在最开始讨论支持向量机的时候,我们就假定,数据是线性可分的,亦即我们可以找到一个可行的超平面将数据完全分开.后来为了处理非线性数据,使用 Kernel 方法对原来的线性 SVM 进行了推广,使得非线性 ...

  9. DFT设计绪论

    DFT设计的主要目的是为了将defect-free的芯片交给客户. 产品质量,通常使用Parts Per million(PPM)来衡量. 但是随着IC从SSI到VLSI的发展,在test上花销的时间 ...

  10. 如何写一个c++插件化系统

    1.为什么需要插件化系统 “编程就是构建一个一个自己的小积木, 然后用自己的小积木搭建大系统”. 但是程序还是会比积木要复杂, 我们的系统必须要保证小积木能搭建出大的系统(必须能被组合),有必须能使各 ...