Aggressive cows

Time Limit: 1000MS Memory Limit: 65536K Total

Submissions: 28666 Accepted: 13146 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

解题思路

题意:把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(二分法)的更多相关文章

  1. 二分法的应用:最大化最小值 POJ2456 Aggressive cows

    /* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

  2. POJ2456 Aggressive cows

    Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...

  3. [ACM] poj 2456 Aggressive cows (二分查找)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 D ...

  4. POJ 2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11192   Accepted: 5492 ...

  5. BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )

    最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...

  6. 疯牛-- Aggressive cows (二分)

    疯牛 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小 ...

  7. 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

    1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 217  Solved: ...

  8. 最大化最小值 Aggressive cows

    Aggressive cows http://poj.org/problem?id=2456 N间小屋,M头牛,使得牛跟牛之间的距离最远,以防止牛打架. 2<=N<=100000 2< ...

  9. poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分

    poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...

随机推荐

  1. Let’s Encrypt https证书安装

    我的博客: https://www.seyana.life/post/15 现在已经有很多的免费ssl证书提供商,国内的也有, 不过国内政策要求还要把key给他们, 我们还是用Let's Encryp ...

  2. 获取的ajax方法return的返回值的问题解析

    今天刚上班就偶遇关于获取Ajax方法return的返回值的问题,这里小记一下. 在使用jquery中,如果获取不到ajax返回值,原因有二: 一.ajax未使用同步 ajax未使用同步,导致数据未加载 ...

  3. Ubuntu 16 编译装python2.7

    Ubuntu 16自带 python3 ,有很多库无法使用,决定装 python2.7 试了试,好像 apt-get 装不上. 使用源码,编译安装. wget https://www.python.o ...

  4. 简说python之安装

    Python是跨平台程序语言,做为世界流行的语言之一,它可以平滑地部署在Windows,Linux,Mac等平台之上,并有很多第三方模块的函数可供使用. 学习Python,首先需要把Python的编译 ...

  5. MySQL记录操作(增删改)

    概览 MySQL数据操作: DML 在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用DELETE实现数据 ...

  6. 事务特性,事务的隔离级别以及spring中定义的事务传播行为

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  7. 你会无聊到把Administrator用户禁用,并且自己创建的用户搞到消失掉还有Administrator用户被禁吗。。。。。

    2020年3月17日20:07:00 如文章标题哈,就是这么任性,无奈 事件起因:因为要部署项目,并且需要将线上的Oracle数据库导入到本地Oracle数据库中突然发现使用 sqlplus 连接数据 ...

  8. Element UI中的上传文件功能

    上传文件给后台: <el-upload style="display:inline-block" :limit=" class="upload-demo& ...

  9. 两个div,高度都是100% 用 display:flex; 和 min-height 一边撑高了,另一边自动走 (不加flex不自动撑开)

    两个div,高度都是100% 用 display:flex; 和 min-height 一边撑高了,另一边自动走

  10. MVVM相关框架

    Caliburn.Micro PropertyChanged.Fody Prism MVVMLight (未完)