poj 2456 Aggressive cows(二分搜索之最大化最小值)
Description
Farmer John has built a new long barn, with N ( <= N <= ,) stalls. The stalls are located along a straight line at positions x1,...,xN ( <= xi <= ,,,). His C ( <= 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 : Two space-separated integers: N and C * Lines ..N+: Line i+ contains an integer stall location, xi
Output
* Line : One integer: the largest minimum distance
Sample Input
Sample Output
Hint
OUTPUT DETAILS: FJ can put his cows in the stalls at positions , and , resulting in a minimum distance of . Huge input data,scanf is recommended.
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
#define N 100006
#define inf 1<<32
int x[N];
int n,m;
bool solve(int d){
int cnt=;
int last=;
for(int i=;i<n;i++){
if(x[i]-x[last]>=d) {
cnt++;
last=i;
}
}
if(cnt>=m) return false;
return true;
}
int main()
{ while(scanf("%d%d",&n,&m)==){
for(int i=;i<n;i++){
scanf("%d",&x[i]);
}
sort(x,x+n);
int low=;
int high=x[n-]-x[];
while(low<high){
int mid=(low+high)>>;
if(solve(mid)){
high=mid;
}
else{
low=mid+;
}
}
printf("%d\n",low-);
}
return ;
}
poj 2456 Aggressive cows(二分搜索之最大化最小值)的更多相关文章
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
- 二分搜索 POJ 2456 Aggressive cows
题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...
- POJ - 2456 Aggressive cows 二分 最大化最小值
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18099 Accepted: 8619 ...
- POJ 2456 Aggressive cows ( 二分搜索)
题目链接 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The ...
- POJ 2456 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- POJ 2456 Aggressive cows (二分)
题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...
- POJ 2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11192 Accepted: 5492 ...
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- POJ 2456 Aggressive cows(贪心 + 二分)
原题链接:Aggressive cows 题目大意:农夫 建造了一座很长的畜栏,它包括 个隔间,这些小隔间依次编号为. 但是, 的 头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...
随机推荐
- 命令行运行android模拟器
创建模拟器 android create avd --name avd_4.1 --target "android-16" --abi armeabi-v7a Android 4. ...
- 坑爹的vector iterators incompatible错误(VS中属性页-->C/C++-->代码生成-->>运行库)
之前一直被这个错误折磨着,就是不知道问题在那,后来找了很多资料,大概都是说这是因为多个线程同时操作vector的问题(参考这里).可是我这里的代码并没有问题,因为同样的代码在别的解决方案中已经成功运行 ...
- Java基础知识强化47:StringBuffer类之StringBuffer的三个面试题
1. 面试题:String,StringBuffer,StringBuilder的区别 ? 答:String是字符串内容不可变的,而StringBuffer和StringBuilder是字符串内容长度 ...
- C编程风格.
C语言编程风格. 关于编程风格,不同书上有不同规范,不同公司都有自己的一套定义.根据自己的编程习惯做个简要说明. 1.变量定义 在定义变量时,前缀使用变量的类型,之后使用表现变量用途的英文单词或单词缩 ...
- com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException idea 导出war 报错
com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException 部署到in ...
- nginx反向代理nginx,RealServer日志打印真实ip
title: nginx反向代理nginx,RealServer日志打印真实ip date: 2016-05-11 19:15:37 tags: --- nginx反向代理nginx,RealServ ...
- javascript 冒泡和事件源 形成的事件委托
冒泡:即使通过子级元素的事件来触发父级的事件,通过阻止冒泡可以防止冒泡发生. 事件源:首先这个东西是有兼容行问题的,当然解决也很简单. 两者结合使用,形成的事件委托有两个优势: 1.减少性能消耗: 2 ...
- Errore HTTP 404.2 - Not Found" IIS 7.5 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理
解决方法: 找到IIS的根节点->右侧“ISAPI和CGI限制”->把禁止的ASP.Net版本项设置为允许. 如下图 今天配置本地iis出现了一些问题,第一个是出现cgi等错误,iis重新 ...
- Asp.Net--回调技术
实现回调技术需要以下步骤: 1.实现ICallbakEventHandler 2.实现接口中的方法:RaiseCallbackEvent 3.实现GetCallbackResult 方法 解释 参数 ...
- QT VS2008未处理的异常: 0xC0000005
症状如图所示 出错代码段在第3行 QString dir = QFileDialog::getExistingDirectory(this, tr("Save file path&qu ...