POJ2456(最大化最小值)
2016-07-17 09:54
99 查看
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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10728 | Accepted: 5288 |
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 思路:最大化最小值、最小化最大值等问题常用二分搜索解决。
#include <iostream> #include <algorithm> using namespace std; const int MAXN=100005; int n,m; int x[MAXN]; bool test(int d) { int cnt=1; int last=x[0]; for(int i=1;i<n;i++) { while(i<n&&x[i]-last<d)//两牛舍之间的距离均不小最小值d { i++; } if(i==n) break; cnt++; last=x[i]; } return cnt>=m; } int main() { while(cin>>n>>m) { for(int i=0;i<n;i++) { cin>>x[i]; } sort(x,x+n); int l=0; int r=0x3f3f3f3f; while(r-l>1) { int mid=(l+r)>>1; if(test(mid)) l=mid; else r=mid; } cout<<l<<endl; } return 0; }
相关文章推荐
- 高效运维最佳实践(03):Redis集群技术及Codis实践
- JUMA SMP API
- linux系统日志与网络管理
- Junit中Assert.assertEquals()和Assert.assertSame方法有什么异同
- 支付宝支付流程
- poj2992(Divisors)(阶乘因数分解)
- 省市联动的简单实现
- cordova 一个将web应用程序封装成app的框架
- 办公神器,专治低效——特色功能软件工具
- 父元素高度为auto,子元素使用top:-50%没有效果的问题
- cordova 一个将web应用程序封装成app的框架
- yarn-cluster运行spark streaming问题
- PHP empty()函数:Can't use method return value in write context
- NerdTree 学习
- html5页面中 触发 拨打电话、发短信 的方式
- java nio的基本原理
- 使用和写简单的Makefile文件
- URL与资源
- 微信的redirect_uri参数错误原因分析
- matlab显示并保存RGB图像的单个通道图像