二分算法的应用——最大化最小值 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> ...
随机推荐
- BeanCreationException报错启动不起来(jar包冲突)
一月 23, 2015 3:46:13 下午 org.apache.catalina.core.AprLifecycleListener init 信息: The APR based Apache T ...
- 图论 Kruskal算法 并查集
#include<iostream> #include<cstring> #include<string> #include<cstdio> #incl ...
- NABCD(网上投票系统)
网上投票系统 N(need) 投票这件事情,在所有事情上都可能用得到,在互联网的影响下,投票的范围变得越来越广,比如在商业的里,往往要做市场分析,那么在互联网这个大的前提下,用网上投票系统来获取用户的 ...
- 生命周期事件和 Global.asax 文件
文章:IIS 5.0 和 6.0 的 ASP.NET 应用程序生命周期概述 该文章有介绍Application_Start方法
- Good Time 冲刺 一
2018/6/14 我们组之前没有开发小程序的经验,所以在尝试中不断探索与学习.在完成小程序的初步注册和界面完善后,我们组开始进行开发任务. 1.1成员简述: 王怡镔:“今天主要学习小程序开发知识及相 ...
- [二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)
1119. Pre- and Post-order Traversals (30) Suppose that all the keys in a binary tree are distinct po ...
- ASP.NET MVC 5.0 参考源码索引
http://www.projky.com/asp.netmvc/5.0/Microsoft/AspNet/Mvc/Facebook/FacebookAppSettingKeys.cs.htmlhtt ...
- pygame学习笔记(4)——声音
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi pygame.mixer是一个用来处理声音的模块,其含义为“混音器”.游戏中对声音的处理一般包括制造声音和播放声音 ...
- 【大数据】Kafka学习笔记
第1章 Kafka概述 1.1 消息队列 (1)点对点模式(一对一,消费者主动拉取数据,消息收到后消息清除) 点对点模型通常是一个基于拉取或者轮询的消息传送模型,这种模型从队列中请求信息,而不是将消息 ...
- 5W2H方法
5W2H分析方法也叫七问分析法,是二战中美国陆军兵器修理部首创.简单.方便.易于理解.使用,富有启发意义,被广泛应用于企业管理和技术活动,对于决策和执行性的措施也非常有帮助,有助于弥补考虑问题的疏漏 ...