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. Python_字符串相关操作

    1.字符串切片操作: str1='hello word' startIndex=0 #开始索引位置 endIndex=5 #结束索引位置+1 step=2 #步长 print(str1[startIn ...

  2. UltraISO 软碟通注册(亲测!!!)

    声明:本文仅限学习者共享使用,请勿用来商业行为,否则后果自负!!! 强烈建议!!!支持正版购买通道,请走这里. 免费下载UltralSO软碟通   首先当然是下载安装UltraISO软件,不然怎么** ...

  3. 每日一题-——LeetCode(486) 预测赢家

    题目描述: 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到 ...

  4. ACM-ICPC 2018 徐州赛区网络预赛 A. Hard to prepare (组合数学,递归)

    A. Hard to prepare After Incident, a feast is usually held in Hakurei Shrine. This time Reimu asked ...

  5. linux网络编程之posix消息队列

    在前面已经学习了System v相关的IPC,今天起学习posix相关的IPC,关于这两者的内容区别,简单回顾一下: 而今天先学习posix的消息队列,下面开始: 接下来则编写程序来创建一个posix ...

  6. PHP启动php-fpm成功,但php-cgi进程查找不到 502 getaway

    一般情况大家刚把lnmp环境安装好之后,把nginx中 fastcgi_pass unix:/tmp/php-cgi.sock项修改成 fastcgi_pass 127.0.0.1:9000之后,网页 ...

  7. 4.Python 进制和位运算

    .button, #logout { color: #333; background-color: #fff; border-color: #ccc; } span#login_widget > ...

  8. Selenium常用API的使用java语言之4-环境安装之Selenium

    1.通过jar包安装 点击Selenium下载 链接 你会看到Selenium Standalone Server的介绍: The Selenium Server is needed in order ...

  9. springcloud系列

    1.使用Spring Cloud搭建服务注册中心2.使用Spring Cloud搭建高可用服务注册中心3.Spring Cloud中服务的发现与消费4.Eureka中的核心概念5.什么是客户端负载均衡 ...

  10. 原生JS实现购物车全选多选按钮功能

    对于JS初学者来说,一个完整的购物车实现还是挺难的,逻辑功能挺多.写出完整功能,能提升不少JS基础,下面实现购物车全选多选按钮功能: 首先HTML及CSS部分: <style> table ...