POJ2456--Aggressive cows(Binary Search) unsolved
Description
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
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
Sample Input
5 3
1
2
8
4
9
Sample Output
3
Hint
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.
#include<iostream>
#include<numeric>
#include<algorithm>
using namespace std;
int pos[];
int n,c;
int C(int d){
int last=;
for(int i=;i<c;i++){
int cur=last+;
while(cur<n&&pos[cur]-pos[last]<d)
cur++;
if(cur==n)
return false;
last=cur;
}
return true;
}
int main(){
cin>>n>>c;
for(int i=;i<n;i++)
cin>>pos[i];
sort(pos,pos+n);
int lb=,ub=*max_element(pos,pos+n);
int mid=(ub-lb)/;
while(ub-lb>=){
if(C(mid)){
lb=mid+;
}
else{
ub=mid-;
}
mid=(lb+ub)/;
}
cout<<mid<<endl;
return ;
}
POJ2456--Aggressive cows(Binary Search) unsolved的更多相关文章
- 二分法的应用:最大化最小值 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[ ...
- 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
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...
- 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 ...
- POJ2456 Aggressive cows(二分+贪心)
如果C(d)为满足全部牛之间的距离都不小于d. 先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d. #include<iostream> #include<cstdio> ...
- POJ2456 Aggressive cows(二分)
链接:http://poj.org/problem?id=2456 题意:一个数轴上n个点,每个点一个整数值,有c个奶牛,要放在这些点的某几个上,求怎么放可以使任意两个奶牛间距离的最小值最大,求这个最 ...
- 【POJ - 2456】Aggressive cows(二分)
Aggressive cows 直接上中文了 Descriptions 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x ...
随机推荐
- bloomfilter 以及count min sketch
bloomfilter http://blog.csdn.net/v_july_v/article/details/6685894 count min sketch http://www.cnblog ...
- linux 使用笔记3
解决linux下打开txt乱码问题 在Linux下要阅读windows生成的txt文件,需要通过iconv进行字符转化 iconv -f gb2312 -t utf8 ./读书笔记.txt > ...
- BZOJ1084或洛谷2331 [SCOI2005]最大子矩阵
BZOJ原题链接 洛谷原题链接 注意该题的子矩阵可以是空矩阵,即可以不选,答案的下界为\(0\). 设\(f[i][j][k]\)表示前\(i\)行选择了\(j\)个子矩阵,选择的方式为\(k\)时的 ...
- 高级设计总监的设计方法论——5W1H需求分析法 KANO模型分析法
本期开始进入设计方法论的学习,大湿自己也是边学边分享,算是巩固一遍吧: 另外这些理论基本都是交叉结合来应用于工作中,我们学习理论但不要拘泥于理论的框架中,掌握后要灵活运用一点- 这些理论一部分来自于我 ...
- walsh矩阵
矩阵A(1) 1 矩阵A(2) 1 1 1 -1 矩阵A(2n) 由上一级矩阵组合,即用上一级矩阵的一行生成本级矩阵的一行. 生成规则是 A(2n)[k] = { A(n)[k], A(n)[k] ...
- GC垃圾收集器分类
参考https://blog.csdn.net/tjiyu/article/details/53983650 Java垃圾收集器组合: 新生代收集器:Serial.ParNew.Parallel Sc ...
- 海港(NOIP2016)
题目链接:海港 这一题怎么样呢?还好吧,也不是太难,没有用到什么特殊的算法,但写法还是很值得学习的.下面讲一下思路: 我们维护三个队列(这里我们采用自己手写的队列,因为这比STL的要快,不过这一题,S ...
- 再读c++primer plus 005
对象和类: 1.类和结构的唯一区别是结构的默认访问类型是public,而类为private: 2.其定义位于类声明中的函数都将自动成为内联函数,也可以在类声明外定义成员函数,并使其成为内联函数,为此只 ...
- Rest架构风格的实践(使用通用Mapper技术)
1.根据用户 id 查询用户数据 1.1 controll控制器 @RequestMapping("restful/user") @Controller public class ...
- 使用Spring 简化MyBatis
1.导入mybatis所有的jar 和 spring 基本包,spring-jdbc,spring-tx,spring-aop,spring整合mybatis的包等. 2.编写spring配置文件ap ...