Aggressive cows(二分法)
Aggressive cows
Time Limit: 1000MS Memory Limit: 65536K Total
Submissions: 28666 Accepted: 13146 DescriptionFarmer 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
解题思路
题意:把m有牛放入n个牛棚,要求让每头牛值简单距离尽可能的大,输出,m头牛之间的 最小的那个距离。
思路 :这题看着很难想,看着数据那么大不知道怎么做,但是只要我要应用二分的思想我们就可以非常容易的解决这个题了:首先我们规定 最小距离为 mn,最大距离 mx为1e9,距离的中间值mid = (mn + mx)/ 2,那么 我们假设 mid就是每头cow之间间隔的最小距离,有了这个假设距离mid,我们在遍历一下所给的牛棚看能不能把所有的奶牛以 mid间距为限制条,能不能把所有的m头奶牛放进去,如果可以就用 ans记录这个可行的mid答案(但不一定是最的答案),这个时候为了求最有优答案,我们再次做假设,能不能假设 奶牛的间距跟大一些呢??,这个时候我们可以调整最小距离的下限mn为mid + 1,这样在二分的时候,我们假设的距离mid自然而然的变成了 mid = (mn + mx) / 2,假设个距离变得更大了,这样再次遍历牛棚看能不能把所有的cow放下,如果能那就继续改变距离下限mm以增大我们假设个距离 mid;如果不能能放下所有的奶牛,说明我们那假设的距离mid 太大了,那么我们就调整上限 让mx = mid - 1 , 来缩小我么你假设的距离,。。。。。。。这样通过不断的缩小我们就可以得到最后的答案了
Aggressive cows(二分法)的更多相关文章
- 二分法的应用:最大化最小值 POJ2456 Aggressive cows
/* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- POJ2456 Aggressive cows
Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...
- [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 ...
- BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...
- 疯牛-- Aggressive cows (二分)
疯牛 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小 ...
- 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 217 Solved: ...
- 最大化最小值 Aggressive cows
Aggressive cows http://poj.org/problem?id=2456 N间小屋,M头牛,使得牛跟牛之间的距离最远,以防止牛打架. 2<=N<=100000 2< ...
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
随机推荐
- RedisTemplate:我不背锅,是你用错了
今天分享一个RedisTemplate的问题,感兴趣的可以继续看下去了,不感兴趣的继续撩妹去吧! 如下图:一位朋友给了我一个报错的图片,为啥为啥取不到值? 我也有点懵,第一反应就是RedisTempl ...
- 将url转化成file文件
let img = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=508387608,28489740 ...
- 使用增量备份修复DG中的GAP
问题描述 oracle中DG出现主备不同步现象,alert日志报警有gap信息,但是v$archive_gap视图查不到任何信息.同时主库上的对应归档已经删除且没有备份 解决方案 1.查询备库的scn ...
- swoft 上传图片到 阿里云oss aliyun-oss
1.swoft 获取上传的文件 .官方文档上面没有看到 $files = $request->getUploadedFiles(); $file = $files['file']; 2.在模型 ...
- 2020 webstorm 最新激活方式 有效期2021年11月 可用
MIIElT25XE-eyJsaWNlbnNlSWQiOiJPUVQzT0oyNVhFIiwibGljZW5zZWVOYW1lIjoi5rC45LmF5r+A5rS7IGlkZWEubWVkZW1pb ...
- openwrt MT7620A MT7610E 5G 驱动添加移值
使用 github 上别人提供好的源码.整合到最新的 openwrt 18 中,目前 kernel 的版本为 4.1 . 编辑中....
- 爬虫使用中间代理人 fiddl...,charles,mitmproxy 设置
一般的设置在网上就能找到(端口,ip啥的) 但是难点是关于安卓手机证书 在网上找到的几种方法,一种是在app源码中添加设置让手机app同意你下载安装的证书,另一种则是root_adb 安装证书 但是太 ...
- 查看chrome插件源码
简介 想查看chrome插件的源码,就需要找到chrome插件安装的位置,接着再文件夹下查找此插件的id. mac cd ~/Library/Application Support/Google/Ch ...
- 自动控制理论的MATLAB仿真实例(一)
拉普拉斯变换及其反变换 Laplace变换及其反变换的定义为:
- JsonFormat 日期少了8个小时?还我
JsonFormat 后日期少了8个小时什么鬼? 前言 今天测试的时候发现时间对不上,比数据库里的时间少了8个小时?测试小姐姐一顿狂轰乱炸,一点都不温柔. 什么鬼?哪里出了问题?数据库显示的是下面