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.有限假设类情形 对于Chernoff bound 不等式,最直观的解释就是利用高斯分布的图象.而且这个结论和中心极限定律没有关系,当m为任意值时Chernoff bound均成 ...

  2. 转 js事件探秘

    Javascript中的事件,可以和html交互. 事件流IE&Opera:事件冒泡其他浏览器: 事件捕获 事件冒泡:事件由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播至 ...

  3. Beta阶段DAY2

    一.提供当天站立式会议照片一张 二.每个人的工作 1.讨论项目每个成员的昨天进展 刘阳航:删除多余按钮,调整界面. 林庭亦:删除麻烦的颜色设置. 郑子熙:添加新增按钮. 陈文俊:重新规划面板及功能. ...

  4. SQL Server学习记录之获取每月每季度每年第一天和最后一天

    DECLARE@dtdatetime SET@dt=GETDATE() DECLARE@numberint --1.指定日期该年的第一天或最后一天 --A. 年的第一天 SELECTCONVERT() ...

  5. 性能分析_linux服务器CPU_中断

    中断 1.  指标范围 1.1  Interrupt rate 应该与cpu利用率结合分析,如果cpu利用率在合理范围内,大量的中断也是可以接受的.一个巨大的中断值,同时伴随着缓慢的系统性能表现,指示 ...

  6. mysql数据优化--数据库结构的优化

    1,比如存时间类型的就使用int类型   其中mysql的两个函数可以拿来使用 unix_timestamp 将时间日期转化为时间戳

  7. [转帖]CPU 的缓存

    缓存这个词想必大家都听过,其实缓存的意义很广泛:电脑整机最大的缓存可以体现为内存条.显卡上的显存就是显卡芯片所需要用到的缓存.硬盘上也有相对应的缓存.CPU有着最快的缓存(L1.L2.L3缓存等),缓 ...

  8. Java 使用 DBCP mysql 连接池 做数据库操作

    需要的jar包有 commons-dbutils , commons-dbcp , commons-pool , mysql-connector-java 本地database.propertties ...

  9. LeetCode 717. 1-bit and 2-bit Characters

    We have two special characters. The first character can be represented by one bit 0. The second char ...

  10. Girls' research HDU - 3294(马拉车水题)

    题意: 求最长回文串 长度要大于等于2  且输出起点和终点  输出回文串字符 这个字符还是要以给出的字符为起点a 输出 解析: 分析一下s_new串就好了 #include <iostream& ...