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.

题目大意

有N间小屋,第i个小屋在xi的位置。

要让人尽可能远离其它人,问最大能安排多大的距离

解题思路

二分查找可行解

记住二分的模板,就以这题的二分为标准

代码

//POJ2456
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100010;
int s[maxn];
int n,m;
bool ok(int x)
{
int cnt = 1;
int tmp = s[0];
for(int i = 1 ; i < n ; i ++) {
if(s[i]-tmp >= x) {
cnt ++;
tmp = s[i];
}
}
return cnt >= m;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = 0 ; i < n ; i ++) scanf("%d",&s[i]);
sort(s,s+n);
int ma = s[n-1]-s[0],mi = 0;
while(ma > mi) {
int mid = mi + (ma-mi+1)/2;
if(ok(mid)) mi = mid;
else ma = mid-1;
}
printf("%d\n",mi);
return 0;
}

POJ2456 Aggressive cows 二分的更多相关文章

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

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

  2. 二分法的应用:最大化最小值 POJ2456 Aggressive cows

    /* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

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

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

  4. POJ2456 Aggressive cows

    Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...

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

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

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

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

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

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

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

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

  9. poj2456 Aggressive cows(二分查找)

    https://vjudge.net/problem/POJ-2456 二分,从最大长度开始,不断折半试,如果牛全放下了,就是可行,修改下界,否则改上届. #include<iostream&g ...

随机推荐

  1. vue及webpack在项目中的一些优化

    传送:https://www.haorooms.com/post/vue_webpack_youhua

  2. LeetCode(52):N皇后 II

    Hard! 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方 ...

  3. 【ES】学习8-聚合1

    参考资料: https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/_combining_the_two.html 特定概念: ...

  4. laravel job 与 event 的区别

    job 是异步执行.适合耗时长的任务.例如,批量发送邮件,短信. event 是在 request 的生命周期内执行.适合耗时短的操作.例如,更改数据字段状态. 但是, event 的好处是,可以复用 ...

  5. Python 索引迭代

    1.使用enumerate函数 L = ['Adam', 'Lisa', 'Bart', 'Paul'] for index, name in enumerate(L):     print inde ...

  6. python接口自动化测试十二:对返回的json的简单操作

    # 1.requests里面自带解析器转字典 print(r.json()) print(type(r.json())) # 取出json中的'result_sk_temp'字段 # {"r ...

  7. Android 倒计时按钮,倒计时发送短信验证码…

    Android基础之——CountDownTimer类,轻松实现倒计时功能https://www.cnblogs.com/yfceshi/p/6853746.html android中获取验证码后出现 ...

  8. 项目部署到liunx环境下访问接口返回异常

    1.访问接口返回异常 已经连续踩了两次这个坑了.所以记下来了.方便下次搜索! 项目在window下运行正常,无任何异常! 但是部署到liunx环境下的服务器上就有问题 访问静态页面毫无问题,一旦涉及到 ...

  9. 【Java】 剑指offer(23) 链表中环的入口结点

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 一个链表中包含环,如何找出环的入口结点?例如,在图3.8的链表中, ...

  10. 短网址服务(TinyURL)生成算法

          前不久做了一个优惠劵的分享功能,其中一个功能就是生成一个优惠劵分享短链接.生成的短链接要求每个链接都是唯一的,并且长度尽可能短.在网上查了一下相关的思路,发现了一个不错的算法.这个算法的思 ...