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 ...
随机推荐
- .NET Best Practices
Before starting with best practices tobe followed, it is good to have clear understanding of how mem ...
- 新建maven指定jdk版本-eclipse新建maven项目报错The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path的解决方案
具体表现为: 使用Eclipse+Maven建立了一个Javaweb工程,JSP页面顶端出现“红色”的报错信息:The superclass "javax.servlet.http.Http ...
- Mac terminal Javac
Mac terminal Javac Open the Terminal's vim , then write them: public class test{ public static void ...
- js sendBeacon
页面性能日志: DNS解析耗时 TCP链接耗时 SSL安全链接耗时 网络请求耗时 DOM解析耗时 资源加载耗时 首包时间 白屏时间 首次可交换时间 Dom Ready时间 页面完全加载时间. 如某些统 ...
- VC设置cookies实现文件刷下载量
VC设置cookies实现文件刷下载量 VC设置cookies实现文件刷下载量 分类: c/c++2013-10-11 17:22 250人阅读 评论(0) 收藏 举报 目录(?)[+] 同学 ...
- Pytest 生成Report
1. 生成JunitXML 格式的测试报告 JunitXML报告是一种很常用的测试报告,比如可以和Jenkins进行集成,在Jenkins的GUI上显示Pytest的运行结果,非常便利.运行完case ...
- 6 云计算系列之Nova安装与配置
preface 上面安装好了glance,下面就开始部署nova计算服务了. nova组件介绍 首先介绍下nova各个组件. api 用来接收和响应外部的请求唯一途径,支持Openstack api, ...
- IT运维助力业务增值
随着业务的不断扩展及IT的深化融合,IT运维在企业日常管理中的地位已经显得越发重要.然而,日常的运维工作繁琐.辛苦,还得不到认可.“吃力不讨好!”也成为很多兢兢业业的IT管理人员普遍存在的苦恼. ...
- Images.xcassets 的简单使用
总结起来Images.xcassets 就这么几句话 (1)以下開始简单整理 进入images.xcassets 右键--->New Image Set 或者点XCode的images.xcas ...
- Linux oracle数据库创建表空间、用户并赋予权限
管理员用户登录oracle数据库 1.创建临时表空间 select name from v$tempfile;查出当前数据库临时表空间,主要是使用里面的存放路径: 得到其中一条记录/opt/oracl ...