POJ 2456 Aggressive cows (牛舍安放_二分+最大化最小值)
2015-09-24 17:59
507 查看
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
Sample Output
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.
注意最后要去l,,,区间范围【l,r)
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.
注意最后要去l,,,区间范围【l,r)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> const int maxn=1e5+100; using namespace std; int a[maxn],n,m; bool judge(int x) { int l,r,t,i; l=1;r=1;t=1; while(r<=n) { while(r<=n&&a[l]+x>a[r]) r++; if(r>n) break; t++; l=r; if(t==m) break; } if(t==m) return true; else return false; } int main() { int i,j,num,sum,mid; scanf("%d%d",&n,&m); sum=0; for(i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+1+n); num=a /(m-1); int l=0,r=num; while(r-l>1) { mid=(l+r)/2; if(judge(mid)) l=mid; else r=mid; } printf("%d\n",l); return 0; }
相关文章推荐
- protothread学习
- apue-intro
- poj3169
- HP-UX 常用命令
- epoll详解
- Android性能优化
- 【IOS 开发学习总结-OC-17】★★objective-c面向对象之——多态
- IE6 的兼容相关问题
- 贷款计算公式
- oracle中的sql%found、sql%notfound、sql%rowcount和sql%isopen
- 内存管理
- rand,srand,random,srandom
- C++ 虚函数实现:虚函数表 虚表指针
- kmp模式串一求next值
- 下载Android 源码(高墙之下,只能用镜像啦)
- Android电话拨打流程源码分析
- Chrome占用CPU很高的解决办法
- ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
- OpenCV一些函数
- rc.local