Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7083   Accepted: 3522

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.

Source

 
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
using namespace std;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; int pos[MS];
int N,M; bool judge(int d)
{
int last=;
int now;
for(int i=;i<M;i++) //贪心法给第i头牛找宿舍
{
now=last+;
while(now<N&&pos[now]-pos[last]<d)
now++;
if(now>=N)
return false;
last=now;
}
return true;
} void solve()
{
sort(pos,pos+N);
int l=,r=INF;
while(r-l>) // 注意是区间,很多时候开区间更方便一些。
{
int mid=(l+r)/;
if(judge(mid))
l=mid;
else
r=mid;
}
printf("%d\n",l);
} int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
for(int i=;i<N;i++)
scanf("%d",&pos[i]);
solve();
}
return ;
}

Aggressive cows 二分不仅仅是查找的更多相关文章

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

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

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

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

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

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

  4. [POJ] 2456 Aggressive cows (二分查找)

    题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...

  5. POJ2456 Aggressive cows 二分

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

  6. [poj 2456] Aggressive cows 二分

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

  7. Aggressive Cows 二分

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

  8. POJ2456 Aggressive cows(二分+贪心)

    如果C(d)为满足全部牛之间的距离都不小于d. 先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d. #include<iostream> #include<cstdio> ...

  9. poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...

随机推荐

  1. C++11常量表达式

    [C++11之常量表达式] 关键字:constexpr: 中文学名:常量表达式. constexpr用于把运行期计算放置在编译期. 使用constexpr有3个限制: 1.函数中只能有一个return ...

  2. RS-232-C串口通讯协议解析(硬件接口协议)

    http://www.dz3w.com/info/interface/0075524.html http://wenku.baidu.com/view/02cc247c27284b73f24250e3 ...

  3. junit学习(3.x)

    自动化测试 测试所有测试类 import junit.framework.TestCase; import junit.framework.Assert; /** *测试类必须要继承TestCase类 ...

  4. Python之函数进阶

    本节内容 上一篇中介绍了Python中函数的定义.函数的调用.函数的参数以及变量的作用域等内容,现在来说下函数的一些高级特性: 递归函数 嵌套函数与闭包 匿名函数 高阶函数 内置函数 总结 一.递归函 ...

  5. Adding Multithreading Capability to Your Java Applications

    src: http://www.informit.com/articles/article.aspx?p=26326&seqNum=3 Interrupting Threads A threa ...

  6. PostgreSQL中 AnyElement AnyArray AnynonArray的区别与联系

    http://www.postgresql.org/docs/current/static/extend-type-system.html#EXTEND-TYPES-POLYMORPHIC 先看一个例 ...

  7. C++ map注意事项

    1.在map中,由key查找value时,首先要判断map中是否包含key. 2.如果不检查,直接返回map[key],可能会出现意想不到的行为.如果map包含key,没有问题,如果map不包含key ...

  8. javascript 操作 excel 全攻略

    最近做一个项目,用到了javascript操纵excel以生成报表,下面是标有详细注解的实例 <html> <head><script language="ja ...

  9. SAP Connector 3.0 .Net 开发

    在VS2010中使用控制台应用程序使用SAP Connector 3.0开发时,当程序运行到实例化RfcConfigParameters时报错 (RfcConfigParametersrefcon = ...

  10. CodeForces 164A Variable, or There and Back Again 搜索

    Variable, or There and Back Again 题目连接: http://codeforces.com/problemset/problem/164/A Description L ...