POJ 2456 Aggressive cows(二分答案)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 22674 | Accepted: 10636 |
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.
Source
【题意】
农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <=
1,000,000,000). 但是,John的C (2 <= C <=
N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗。为了不让牛互相伤害。John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢
【分析】
贪心+二分
二分枚举相邻两牛的间距,判断大于等于此间距下能否放进所有的牛。
【代码】
#include<cstdio>
#include<algorithm>
#include<iostream>
#define debug(x) cerr<<#x<<" "<<x<<'\n';
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int N=1e5+5;
int ans,n,m,d[N];
inline bool check(int now){
int res=1,p=d[1];
for(int i=2;i<=n;i++)
if(d[i]-p>=now)
res++,p=d[i];
return res>=m;
}
int main(){
n=read(),m=read();
for(int i=1;i<=n;i++) d[i]=read();
sort(d+1,d+n+1);
int l=0,r=d[n],mid;
while(l<=r){
mid=l+r>>1;
if(check(mid)){
ans=mid;
l=mid+1;
}
else{
r=mid-1;
}
}
printf("%d\n",ans);
return 0;
}
POJ 2456 Aggressive cows(二分答案)的更多相关文章
- POJ 2456 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- POJ - 2456 Aggressive cows 二分 最大化最小值
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18099 Accepted: 8619 ...
- poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...
- POJ 2456 Aggressive cows ( 二分 && 贪心 )
题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...
- [poj 2456] Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- 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 Agressive cows(二分)
POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...
随机推荐
- IDEA添加非空Getter方法模板
#if($field.modifierStatic) static ## #end $field.type ## #set($name = $StringUtil.capitalizeWithJava ...
- Eclipse安装图形JFrame,Jswing编程界面
打开eclipse,选择help--->install new software 来源http://www.cnblogs.com/xiaobo-Linux/p/7954274.html 打开网 ...
- touch事件的分发和消费机制
Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev).onInterceptTouchEvent(MotionEvent ev). ...
- Linux 查看系统硬件信息
linux查看系统的硬件信息,并不像windows那么直观,这里我罗列了查看系统信息的实用命令,并做了分类,实例解说. cpu lscpu命令,查看的是cpu的统计信息. blue@blue-pc:~ ...
- Android图片编码机制深度解析(Bitmap,Skia,libJpeg)
问题 工作中遇到了Android中有关图片压缩保存的问题,发现这个问题还挺深,而且网上资料比较有限,因此自己深入研究了一下,算是把这个问题自顶至下全部搞懂了,在此记录. 相关的几个问题如下: 1.An ...
- spring数据连接池配置参数
1.使用jdbc连接,每次使用完毕需要关闭连接. 2.使用数据库连接池就会方便很多,但是如果参数配置不对,也会引起mysql连接数不够,导致mysql挂掉,基本原理是:不使用的连接数及时回收回来,而不 ...
- 利用ROS工具从bag文件中提取图片
bag文件是ROS常用的数据存储格式,因此要从bag文件中提取数据就需要了解一点ROS的背景知识. 1. 什么是ROS及其优势 ROS全称Robot Operating System,是BSD-lic ...
- mysql主从复制-方案1
mysql主机master 1. 编辑mysql配置文件my.cnf server_id = 1 #server_id服务器唯一标识 log_bin = mys ...
- java 实现类似于python requests包的Session类,自动管理cookie。
1.在py中requests.post()和get()函数都是在那个函数内部里面自动生成了一个Session类的实例,所以requests,post和get函数要想干登陆后才能干的事情,需要添加coo ...
- grid响应式布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...