二分算法的应用——最大化最小值 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> ...
随机推荐
- 第二次作业<1>
1001.A+B Format (20) ac代码 1. 解题思路 先求和,再输出. 答案区间为-2,000,000至2,000,000,将答案分为三个区段分类讨论.虽然觉得很烦但是想不出更好的方法. ...
- DPDK网卡绑定
进入DPDK目录编译环境 # cd ~/DPDK/usertools # ./dpdk-setup.py 注意,setup脚本需要在root权限下运行,并且每次重启电脑,都需要重新插入模块和绑定网卡. ...
- Firefox插件开发学习总结
2018.06.14 我们小组最初只准备开发运行在google上的知乎插件,但我们经过调研发现还有一大部分用户是使用的火狐浏览器,所以我们也准备制作火狐插件.以下是我学习了部分火狐插件制作知识后的总结 ...
- TCP系列55—拥塞控制—18、其他拥塞控制算法及相关内容概述
前面我们演示分析了100+个wireshark TCP实例,拥塞控制部分也介绍常见的拥塞处理场景以及4种拥塞撤销机制,但是我们一直使用的都是reno拥塞控制算法.实际上拥塞控制发展到今天已经有了各种各 ...
- 使用docker-compose编排django、mysql实战
背景: 本萌最近在部署自己开发的项目的时候发现同一套代码上传到服务器上后,部分功能莫名其妙的有点问题,服务器的各项配置都没有做过变动,所以想把项目转战到docker. 奈何刚接触docker,很多地方 ...
- SqlServer日期时间函数
-- 判断是否当天,createdate为日期字段 -- ╔════════════════════╗ -- ============================================= ...
- 浅析Web数据存储-Cookie、UserData、SessionStorage、WebSqlDatabase
Cookie 它是标准的客户端浏览器状态保存方式,可能在浏览器诞生不久就有Cookie了,为什么需要Cookie 这个东东?由于HTTP协议没有状态,所以需要一个标志/存储来记录客户浏览器当前的状态, ...
- 多线程同步与并发访问共享资源工具—Lock、Monitor、Mutex、Semaphore
“线程同步”的含义 当一个进程启动了多个线程时,如果需要控制这些线程的推进顺序(比如A线程必须等待B和C线程执行完毕之后才能继续执行),则称这些线程需要进行“线程同步(thread synchro ...
- Gauss Prime UVA - 1415
题意:给出a和b判定是否为高斯素数 解析: 普通的高斯整数i = sqrt(-1) 高斯整数是素数当且仅当: a.b中有一个是零,另一个是形为或其相反数的素数: 或a.b均不为零,而为素数. 这题 提 ...
- 【转】关于在vim中的查找和替换
1,查找 在normal模式下按下/即可进入查找模式,输入要查找的字符串并按下回车. Vim会跳转到第一个匹配.按下n查找下一个,按下N查找上一个. Vim查找支持正则表达式,例如/vim$匹配行尾的 ...