二分算法的应用——最大化最小值 POJ2456 Aggressive cows
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的更多相关文章
- 二分法的应用:最大化最小值 POJ2456 Aggressive cows
/* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- 最大化最小值 Aggressive cows
Aggressive cows http://poj.org/problem?id=2456 N间小屋,M头牛,使得牛跟牛之间的距离最远,以防止牛打架. 2<=N<=100000 2< ...
- POJ2456 Aggressive cows(二分)
链接:http://poj.org/problem?id=2456 题意:一个数轴上n个点,每个点一个整数值,有c个奶牛,要放在这些点的某几个上,求怎么放可以使任意两个奶牛间距离的最小值最大,求这个最 ...
- POJ2456 Aggressive cows
Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...
- POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13993 Accepted: 6775 ...
- POJ2456 Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- poj2456 Aggressive cows(二分查找)
https://vjudge.net/problem/POJ-2456 二分,从最大长度开始,不断折半试,如果牛全放下了,就是可行,修改下界,否则改上届. #include<iostream&g ...
- 二分算法的应用——最大化平均值 POJ 2976 Dropping tests
最大化平均值 有n个物品的重量和价值分别wi 和 vi.从中选出 k 个物品使得 单位重量 的价值最大. 限制条件: <= k <= n <= ^ <= w_i <= v ...
- POJ2456 Aggressive cows(二分+贪心)
如果C(d)为满足全部牛之间的距离都不小于d. 先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d. #include<iostream> #include<cstdio> ...
随机推荐
- UTC时间与北京时间
经常混淆于此,特地研究了一下,记录在此以备忘. 整个地球分为二十四时区,每个时区都有自己的本地时间.在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal ...
- KEIL C51 printf格式化输出特殊用法
作者:dragoniye 发布:2014-02-15 12:44 分类:硬件 抢沙发 /*******************************************KEI ...
- vue router 几种方式对比 (转载)
<div id="app"> <h1>Hello App!</h1> <p> <!-- 使用 router-link 组件来导 ...
- 10th 本周工作量及进度统计
本周PSP C(类别) C(内容) S(开始时间) ST(结束时间) I(中断时间) T(实际时间) 文档 11月22日 规格说明书练习 10:23 15:30 132 175 11月23日 知识点 ...
- vue组件讲解(is属性的用法)
什么是组件? 在说之前我们先搞清楚什么是组件?这样对我们下边的学习是很有帮助的. 组件(Component)是Vue.js最强大的功能之一.组件可以扩展HTML元素,封装可以重复使用的代码.在较高层次 ...
- mysql group by分组查询
分组的SQL语句有2个: group by 和分组聚合函数实现 partition by (oracle和postgreSQL中的语句)功能 group by + having 组合赛选数据 注意:h ...
- 微信小程序 功能函数 替换字符串内的指定字符
var str = 'abcadeacf'; var str1 = str.replace('a', 'o'); alert(str1); // 打印结果: obcadeacf var st ...
- python3+selenium3+requests爬取我的博客粉丝的名称
爬取目标 1.本次代码是在python3上运行通过的 selenium3 +firefox59.0.1(最新) BeautifulSoup requests 2.爬取目标网站,我的博客:https:/ ...
- 最小费用流spfa算法模板(pascal)
以前写过,现在的码风与以前有些变化,主要是用数组模拟邻接表存图,以前是用指针存图. 以前的博文:http://www.cnblogs.com/Currier/p/6387732.html 洛谷可评测. ...
- Machine Learning CodeForces - 940F(带修改的莫队)
题解原文地址:https://www.cnblogs.com/lujiaju6555/p/8468709.html 给数组a,有两种操作,1 l r查询[l,r]中每个数出现次数的mex,注意是出现次 ...