POJ 2456 Aggressive cows (二分)
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.
#include<stdio.h>
#include<algorithm>
using namespace std;
const int N = ;
int a[N],n,m;
int _is(int x)
{
int cnt=,y=a[];
for (int i=; i<n; i++)
if (a[i]-y>=x)
{
cnt++;
y=a[i];
if (cnt+==m)
return ;
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
int l=,r=a[n-]-a[];
while(l<=r)
{
int mid =l+(r-l)/;
if ( _is(mid) )
l=mid+;
else r=mid-;
}
if (!_is(l)) l--;
printf("%d\n",l);
return ;
}
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: 22674 Accepted: 10636 Des ...
- 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 二分
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 ( 二分 && 贪心 )
题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...
- 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 ...
随机推荐
- git pull 和git fetch区别?
git:从远程分支获取最新的版本至本地有两个命令. git fetch 相当于从远程获取最新的版本至本地,但不会自动merge git pull 相当于从远程获取最新的版本并merge至本地
- Lifecycle mapping "org.eclipse.m2e.jdt.JarLifecycleMapping" Eclipse Maven报错
eclipse 项目中报错Lifecycle mapping "org.eclipse.m2e.jdt.JarLifecycleMapping" is not available, ...
- Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.3.RELEASE:repackage (repackage)
解决方案是删除 pom.xml配置的问题 <build> <plugins> <plugin> <groupId>org.springframework ...
- 使用jackson转json解决双向关联循环调用
ITOO V1.0的开发算是告一段落了,现在是整理总结交接环节,在这个项目中常见的问题也该好好整理一下和大家分享了,这次主要介绍转json循环调用的问题. 一.问题背景 相信只要使用ORM映射实体关联 ...
- Python工程编译成跨平台可执行文件(.pyc)
原文:https://blog.csdn.net/zylove2010/article/details/79593655 在某些场景下,若不方便将python编写的源码工程直接给到其他人员,则可以将p ...
- java.util.NoSuchElementException: No value present
错误: java.util.NoSuchElementException: No value present 原因: 经查询博客Java 8 Optional类深度解析发现,究其原因为: 在空的Opt ...
- H3C RIPv2配置举例
- MAMP "403 Forbidden You don't have permission to access / on this server."
2015年01月22日 17:27:31 阅读数:3488 用MAMP搭建本地服务器的时候,设置好ip和端口等属性之后,浏览器访问,报 403错误: Forbidden You don't have ...
- H3C 环路避免机制一:路由毒化
- java 使用反射调用方法
每个Method的对象对应一个具体的底层方法.获得Method对象后,程序可以使用Method里面的invoke方法来执行该底层方法. Object invoke(Object obj,Object ...