POJ 2456 Aggressive cows ( 二分搜索)
Description
Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000).
His C (2 <= 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 1: Two space-separated integers: N and C
Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
Line 1: One integer: the largest minimum distance
Sample Input
5 3
1
2
8
4
9
Sample Output
3
Hint
OUTPUT DETAILS:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.
Huge input data,scanf is recommended.
分析:
类似最大化最小值或者最小化最大值的问题,通常用二分搜索法就可以很好的解决。
定义:C(d):表示,可以安排牛的位置使得最近的两头牛的距离不小于d
那么问题就变成了求满足C(d)的最大的d。另外,最近的间距不小于d也可以说成是所有牛的间距都不小于d,因此就有,
C(d),表示,可以安排牛的位置使得任意的牛的间距都不小于d
这个问题的判断使用贪心法便可以很好的解决。
1.对牛舍的位置x进行排序
2.把第一头牛放入x0的位置
3.如果第i头牛放入了xj的话,第i+1头牛要放入满足xj+d的最小的xk中,
对x的排序只需要在最开始的时候进行一次就可以了,每一次判断对每头牛最多进行一次处理。
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int N,M;
int x[100009];
bool C(int d)
{
int last=0;
for(int i=1; i<M; i++)///看看这个距离能不能放下
{
int crt=last+1;
while(crt<N&&x[crt]-x[last]<d)
crt++;
if(crt==N) return false;
last=crt;
}
return true;
}
void solve()
{
sort(x,x+N);
int lb=0,ub=0x3f3f3f3f;
while(ub-lb>1)
{
int mid=(lb+ub)/2;
if(C(mid)) lb=mid;
else ub=mid;
}
printf("%d\n",lb);
}
int main()
{
scanf("%d%d",&N,&M);
for(int i=0; i<N; i++)
scanf("%d",&x[i]);
solve();
return 0;
}
POJ 2456 Aggressive cows ( 二分搜索)的更多相关文章
- poj 2456 Aggressive cows(二分搜索之最大化最小值)
Description Farmer John has built a <= N <= ,) stalls. The stalls are located along a straight ...
- 二分搜索 POJ 2456 Aggressive cows
题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
- POJ 2456 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- POJ 2456 Aggressive cows (二分)
题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
- POJ 2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11192 Accepted: 5492 ...
- POJ 2456 Aggressive cows(二分答案)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...
- POJ - 2456 Aggressive cows 二分 最大化最小值
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18099 Accepted: 8619 ...
- poj 2456 Aggressive cows 贪心+二分
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25944 Accepted: 11982 ...
随机推荐
- lintcode-189-丢失的第一个正整数
189-丢失的第一个正整数 给出一个无序的正数数组,找出其中没有出现的最小正整数. 样例 如果给出 [1,2,0], return 3 如果给出 [3,4,-1,1], return 2 挑战 只允许 ...
- Python 零碎信息-基础 01
1. """ 可以插入多行文字. print """ abC 123' 456''" #单引号, 双引号, 也没有关系 " ...
- lol人物模型提取(九)——终章
之前顺丰来人拍照取证了一下,又和卖家协商了一下,最后顺风只打算赔偿我23块钱的顺丰邮寄卡,就是当你寄货物的时候可以用这卡来抵邮钱.想想也没多少钱,对方也不同意微信或支付宝转账给我,索性不让顺丰公司 ...
- 【ASP.NET Core】- 搭建MVC框架
1.使用最新版本的VS2017,并安装.NET Core2.0中相关开发工具 2.打开VS2017,点击文件-新建-项目,选择.NET Core中的ASP.NET Core Web 应用程序 ...
- SERVER 2008 R2 SP1下的内存虚拟盘(支持32位,64位的所有windows版本)
一时冲动把笔记本的系统换成了2008 R2 SP1的,本来想着用笔记本开HYPER-V的,结果是失败了,我发现我的4750G开了HYPER-V后CPU一直居高不下,其中有个什么系统软件保护的服务就占用 ...
- arp_filter/arp_ignore/rp_filter
下面这段代码应该是arp_ignore/arp_filter的最好的注脚;在ARP_ignore通过的情况下,我再去判断ARP_filter,这个ARP_filter其实就是为了判断,当数据包再出去的 ...
- lambda 分组练习
public partial class Form1 : Form { public Form1() { InitializeComponent(); } List<Person> per ...
- ueditor 定制工具栏图标
在使用Ueditor时,如要简化工具栏上的按钮,可以修改配置项的方法: 1. 方法一:修改 ueditor.config.js 里面的 toolbars 2. 方法二:实例化编辑器的时候传入 tool ...
- 【bzoj4842】[Neerc2016]Delight for a Cat 线性规划与网络流
题目描述 $n$ 个连续的位置,每个位置可以填入 S 和 E ,第 $i$ 个位置填入 S 可以获得 $s_i$ 的收益,填入 E 可以获得 $e_i$ 的收益.要求每连续的 $k$ 个位置必须包含至 ...
- BZOJ3437 小P的牧场 【斜率优化dp】
3437: 小P的牧场 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1502 Solved: 836 [Submit][Status][Disc ...