Aggressive cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15528   Accepted: 7440

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.

check(d)=可以安排所有的牛的位置使得最近的两头牛的距离都不小于d

求满足check(d)的最大的d的值。

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
int house[maxn];
int n,m;
const int inf=99999999;
bool check(int d)
{
int last=0;
for(int i=1;i<m;i++)
{
int crt=last+1;
while(crt<n&&house[crt]-house[last]<d)
{
crt++;
}
if(crt==n)return false;
last=crt;
}
return true;
}
void solve()
{
sort(house,house+n);
int lb=0,ub=inf;
while(ub-lb>1)
{
int mid=(ub+lb)/2;
if(check(mid))
lb=mid;
else
ub=mid;
}
printf("%d\n",lb);
} int main()
{
cin>>n>>m;
for(int j=0;j<n;j++)
scanf("%d",&house[j]);
solve();
return 0;
}

最大化最小值poj2456Aggressive cows的更多相关文章

  1. poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分

    poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...

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

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

  3. POJ_2456_Agressive_cows_(二分,最大化最小值)

    描述 http://poj.org/problem?id=2456 有n个小屋,线性排列在不同位置,m头牛,每头牛占据一个小屋,求最近的两头牛之间距离的最大值. Aggressive cows Tim ...

  4. POJ2456(最大化最小值)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10728   Accepted: 5288 ...

  5. POJ3285 River Hopscotch(最大化最小值之二分查找)

    POJ3285 River Hopscotch 此题是大白P142页(即POJ2456)的一个变形题,典型的最大化最小值问题. C(x)表示要求的最小距离为X时,此时需要删除的石子.二分枚举X,直到找 ...

  6. hihocoder 二分·二分答案【二分搜索,最大化最小值】 (bfs)

    题目 这道题做了几个小时了都没有做出来,首先是题意搞了半天都没有弄懂,难道真的是因为我不打游戏所以连题都读不懂了? 反正今天是弄不懂了,过几天再来看看... 题意:一个人从1点出发到T点去打boss, ...

  7. codeforce 1070 E Getting Deals Done(二分求最大化最小值)

    Polycarp has a lot of work to do. Recently he has learned a new time management rule: "if a tas ...

  8. nyoj-586-疯牛|poj-2456-Aggressive cows

    http://acm.nyist.net/JudgeOnline/problem.php?pid=586 http://poj.org/problem?id=2456 解题思路:最大化最小值二分答案即 ...

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

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

随机推荐

  1. JavaScript判断页面是否已经加载完毕

    做页面时经常会遇到当前页面加载完成后,执行某些初始化工作.这时候就要知道如何判断页面(包括IFRAME)已经加载完成,代码如下: < script language = "javasc ...

  2. NOIP 2010 机器翻译

    P1540 机器翻译 题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于 ...

  3. 解决confluence的乱码问题

    使用confluence时发现一些含有中文的页面中,中文都变成了问号. 继续搜索解决方案,发现时数据库中数据的格式不对, 在mysql中输入以下命令:   mysql> show variabl ...

  4. golang 查询数据库操作

    SQL.Open only creates the DB object, but dies not open any connections to the database. If you want ...

  5. 【面试】iOS 开发面试题(一)

      1. #import 跟#include 又什么差别,@class呢, #import<> 跟 #import""又什么差别? 答:#import是Objectiv ...

  6. Replica Sets+Sharding方案之真枪实弹篇

    话说在虚拟机中会报各种错误.在真实的环境中就不会那么痛苦了. 想了一下虚拟机中报错的原因有. 机器卡,处理的时间长就会抛出错误 还有虚拟机假设给的空间太小.也会报异常. 此处讲讲我的大致思路. 第一, ...

  7. poj 3263 Tallest Cow(线段树)

    Language: Default Tallest Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1964   Ac ...

  8. 2015 Multi-University Training Contest 4 1001 Olympiad

    代码: #include<cstdio> #include<cstring> #include<set> using namespace std; int vis[ ...

  9. hdu1162

    #include<cstdio> #include<cmath> #include<climits> #include<algorithm> #defi ...

  10. c# Http下载

    1.首先是服务器上发布资源,如果资源时自定义格式,比如 .zidingyi结尾的后缀文件,需要在MIME类型上添加处理方式 需要注意的是.net里面要设置 System.Net.ServicePoin ...