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. css系列之box-sizing

    转载自:http://zh.learnlayout.com/box-sizing.html 人们慢慢的意识到传统的盒子模型不直接,所以他们新增了一个叫做 box-sizing 的CSS属性.当你设置一 ...

  2. 大型NodeJS项目架构与优化

    使用场景: proxy(API冗余,跨域) vue ssr(服务端渲染) socket(大并发,通讯) 区块链(创业公司,新兴行业) 讨论什么? NodeJS异步IO原理及优化方案 NodeJS内存管 ...

  3. 搭建ssh框架项目(五)

    一.控制层优化 (1)创建BaseAction.java类 package com.cppdy.ssh.web.action; import javax.servlet.http.HttpServle ...

  4. cf1020c 瞎搞

    枚举获胜状态即可 #include<iostream> #include<cstdio> #include<cstring> #include<cmath&g ...

  5. [HNOI2016]序列(未通过)

    题解: 虽然知道有点问题但是并没有debug出来 发现错误了..相同元素的处理有错误 网上题解大都是分块..(hn怎么道道分块) 用最普通的思路,可以枚举每个点作为最小值,向左向右延伸 但是多组询问显 ...

  6. day12--数据库(Mysq)

    1. 数据库介绍 什么是数据库?(https://www.cnblogs.com/alex3714/articles/5950372.html) 数据库(Database)是按照数据结构来组织.存储和 ...

  7. 不一样的go语言-gopher

    前言   gopher原意地鼠,在golang 的世界里解释为地道的go程序员.在其他语言的世界里也有PHPer,Pythonic的说法,反而Java是个例外.虽然也有Javaer之类的说法,但似乎并 ...

  8. IdentityServer4-客户端的授权模式原理分析(三)

    在学习其他应用场景前,需要了解几个客户端的授权模式.首先了解下本节使用的几个名词        Resource Owner:资源拥有者,文中称“user”:        Client为第三方客户端 ...

  9. 由自定义事件到vue数据响应

    前言 除了大家经常提到的自定义事件之外,浏览器本身也支持我们自定义事件,我们常说的自定义事件一般用于项目中的一些通知机制.最近正好看到了这部分,就一起看了下自定义事件不同的实现,以及vue数据响应的基 ...

  10. js基础梳理-内存空间

    我估计有很多像我这样非计算机专业的人进入到前端之后,总是在写业务代码,思考什么什么效果如何实现,导致很多基础概念型的东西都理解得并不太清楚.经常一碰到群里讨论的些笔试题什么的,总觉得自己像是一个假前端 ...