Aggressive cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: Accepted:
Description Farmer John has built a new long barn, with N ( <= N <= ,) stalls. The stalls are located along a straight line at positions x1,...,xN ( <= xi <= ,,,). His C ( <= 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 : Two space-separated integers: N and C * Lines ..N+: Line i+ contains an integer stall location, xi
Output * Line : One integer: the largest minimum distance
Sample Input Sample Output

来源:http://poj.org/problem?id=2456

题意:N个牛舍,第i号牛舍在 xi 的位置。其中 M头牛,对位置不满意,所以,要最大化最近的两头牛的距离。

可以把他看成二分的题目,如之前的写的博客,这种问题关键是 编写 二分的条件bool C(x)。这里的条件C(d):可以安排M只牛的位置,使得最近两只牛的距离不小于d。

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstdlib>
using namespace std; /*
5
3
1 2 8 4 9
*/
const int maxn = + ;
int N, M;
int x[maxn];
int INF; void input()
{
cin >> N >> M;
for (int i = ; i < N; i++) {
cin >> x[i];
INF = max(INF, x[i]);
}
INF++;
} //判断是否满足条件
bool C(int d)
{
int last = ;
for (int i = ; i < M; i++)
{
int next = last + ;
while (next < N && x[next] - x[last] < d) {
next++;
}
if (next == N) return false;
last = next; //遇到间距大于d的,则更新 last,向后继续寻找
}
return true;
} void solve()
{
input(); //最开始对x数组进行排序
sort(x, x + N); //初始化解的范围
int lh = , rh = INF; int mid = ;
while (rh - lh > )
{
mid = (lh + rh) / ;
if (C(mid)) {
lh = mid;
}
else {
rh = mid;
}
}
cout << lh << endl;
} int main()
{
solve();
return ;
}

二分算法的应用——最大化最小值 POJ2456 Aggressive cows的更多相关文章

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

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

  2. 最大化最小值 Aggressive cows

    Aggressive cows http://poj.org/problem?id=2456 N间小屋,M头牛,使得牛跟牛之间的距离最远,以防止牛打架. 2<=N<=100000 2< ...

  3. POJ2456 Aggressive cows(二分)

    链接:http://poj.org/problem?id=2456 题意:一个数轴上n个点,每个点一个整数值,有c个奶牛,要放在这些点的某几个上,求怎么放可以使任意两个奶牛间距离的最小值最大,求这个最 ...

  4. POJ2456 Aggressive cows

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

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

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

  6. POJ2456 Aggressive cows 二分

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

  7. poj2456 Aggressive cows(二分查找)

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

  8. 二分算法的应用——最大化平均值 POJ 2976 Dropping tests

    最大化平均值 有n个物品的重量和价值分别wi 和 vi.从中选出 k 个物品使得 单位重量 的价值最大. 限制条件: <= k <= n <= ^ <= w_i <= v ...

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

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

随机推荐

  1. 【Coursera】支持向量机

    一.最大间隔分类器 1. 函数间隔:\(γ^{i} = y^{i}(w^{T} x + b)\), 改变w和b的量级,对分类结果不会产生任何影响,但是会改变函数间隔的大小.因此,直接对函数间隔求最大值 ...

  2. golang数据类型转换

    int--string //string到int value_int,err:=strconv.Atoi(string) //int到string str:=strconv.Itoa(value_in ...

  3. sql中Union和union all的使用

    该文转载自:http://www.cnblogs.com/chaobaojun/archive/2009/12/24/1631508.html 在MS-SQL如果将两个或更多查询的结果组合为单个结果集 ...

  4. Redis内存回收:LRU算法

    Redis技术交流群481804090 Redis:https://github.com/zwjlpeng/Redis_Deep_Read Redis中采用两种算法进行内存回收,引用计数算法以及LRU ...

  5. [转帖学习]Howto Shrink a Thin Provisioned Virtual Disk (VMDK)

    Howto Shrink a Thin Provisioned Virtual Disk (VMDK) Posted by fgrehl on November 24, 2014Leave a com ...

  6. 实现LinearLayout(垂直布局,Gravity内容排布)

    首先上Gravity的代码,Android原版的Gravity搞得挺复杂的,太高端了.但基本思路是使用位运算来做常量,我就自己消化了一些,按自己的思路来实现. 先上代码,在做分析. package k ...

  7. React之智能组件和木偶组件

    智能组件 VS 木偶组件 在 React + Redux 结合作为前端框架的时候,提出了一个将组件分为“智能”和“木偶”两种 智能组件:它是数据的所有者,它拥有数据.且拥有操作数据的action,但是 ...

  8. 设置快捷键 让word轻松实现无格式粘贴

    设置快捷键 让word轻松实现无格式粘贴使用word时,我们经常会遇到需要将网页上的内容复制到word进行编辑的情况,但是通常这样复制进来的内容都是带有格式的,编辑起来非常不便.虽然我们可以利用“记事 ...

  9. python2 python3共存解决方案

    作者:匿名用户链接:https://www.zhihu.com/question/21653286/answer/95532074来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  10. 【转】VS2012简单的使用感受+插件推荐

    转自:http://blog.sina.com.cn/s/blog_58c506600101b34t.html ~PS:后面有博主自己补充的VS2013的哦~ 1.Visual Studio Achi ...