Aggressive cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24979   Accepted: 11594

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

一句话题意:
有n个牛栏,k头牛。将这k头牛放入牛栏中,如何保证牛与牛之间最小的距离最大。
解析:
明显的是二分算法。
L=0;R= 最大值
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
int n,k;
int a[maxn];
int check(int x){
int cnt=;
int y=a[];
for(int i=;i<=n;i++)
if(a[i]-y>=x){
cnt++;y=a[i];
if(cnt>=k) return ;
}
return ;
}
int main(){
cin>>n>>k;
for(int i=;i<=n;i++){
cin>>a[i];
}
sort(a+,a++n);
int l=,r=a[n],mid;
while (l<r){
mid=(l+r+)/;
if(check(mid)) l=mid;
else r=mid-;
}
cout<<l<<endl;
return ;
}

poj2456的更多相关文章

  1. poj2456 Aggressive cows(二分查找)

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

  2. POJ2456 Aggressive cows 二分

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

  3. POJ-2456 Aggressive cows---最大化最小值(也就是求最大值)

    题目链接: https://vjudge.net/problem/POJ-2456 题目大意: 有n个牛栏,选m个放进牛,相当于一条线段上有 n 个点,选取 m 个点, 使得相邻点之间的最小距离值最大 ...

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

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

  5. POJ2456 Aggressive cows

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

  6. poj2456 二分逼近寻找正确答案

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10078   Accepted: 4988 ...

  7. POJ3258--River Hopscotch(Binary Search similar to POJ2456)

    Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully ...

  8. poj2456(二分+贪心)

    题目链接:http://poj.org/problem?id=2456 题意: 有n个呈线性排列的牲畜堋,给出其坐标,有c头牛,求把两头牛的最短距离的最大值. 思路: 先将坐标排个序.两头牛的最短距离 ...

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

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

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

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

随机推荐

  1. Nginx练习练习玩玩

    Date:2019-11-9 读前思考: 对于NGINX,一般会问什么? 面试官会从哪方面入手? 面试官到底想考察什么? 你做好准备了吗? 如果对于初学者,往往可以通过面试题来提升对某一个技术的了解和 ...

  2. 2019-ACM-ICPC-沈阳区网络赛-K. Guanguan's Happy water-高斯消元+矩阵快速幂

    2019-ACM-ICPC-沈阳区网络赛-K. Guanguan's Happy water-高斯消元+矩阵快速幂 [Problem Description] 已知前\(2k\)个\(f(i)\),且 ...

  3. Codeforces Round #511 (Div. 2) C. Enlarge GCD (质因数)

    题目 题意: 给你n个数a[1]...a[n],可以得到这n个数的最大公约数, 现在要求你在n个数中 尽量少删除数,使得被删之后的数组a的最大公约数比原来的大. 如果要删的数小于n,就输出要删的数的个 ...

  4. java中使用redis --- List列表的简单应用

    1.Dos中启动server端 2.idea中启动client端 public class RedisTest01 { public static void main(String[] args){ ...

  5. python - scrapy 爬虫框架 ( 起始url的实现,深度和优先级,下载中间件 )

    1.  start_urls  --  起始URL 的内部实现(将迭代器转换为生成器) class QSpider(scrapy.Spider): name = 'q' allowed_domains ...

  6. js 数组中如何删除字典

    区别: []表示是一个数组,如var strs = ['a','b','c'].{}表示是一个对象,比如,var obj = {name: '宙斯',sex: 1} 如何在数组中删除指定对象呢?? [ ...

  7. java线程中yield(),sleep(),wait()区别详解

    1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...

  8. 二十八. Ceph概述 部署Ceph集群 Ceph块存储

    client   :192.168.4.10 node1 :192.168.4.11 ndoe2 :192.168.4.12 node3 :192.168.4.13   1.实验环境 准备四台KVM虚 ...

  9. 案例(拖拽对话框、高清放大镜、自制滚动条、元素的隐藏方式、表格隔行变色、tab切换效果、字符串拼接、刷新评论)

    一.拖拽对话框 <style> .of{ width: 500px; } #link,#close{ text-decoration: none; margin: 0 10px; font ...

  10. CSP考前总结

    10.2 考试: 1.数位DP 或者找规律 2.SB题,扫一遍找最大最小即可 3.莫比乌斯反演 出题人相出个数论和数据结构的综合题,但是找不到NOIP级别的,没办法只能忍痛割爱出个莫比乌斯,话说回来, ...