poj 2456 Aggressive cows 贪心+二分
Aggressive cows
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 25944 | Accepted: 11982 |
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<iostream>
#include<algorithm>
#include<math.h>
#define ll long long
using namespace std;
ll a[];
ll n,m;//n是位置数量,m是奶牛数量 int check(ll x)//判断在所有奶牛之中,两两之间的最小距离为x时能否放下所有奶牛
{
ll cnt=,mn=a[];//初始值
for(int i=;i<n;i++)
{
if(a[i]-mn>=x)
{
cnt++;
mn=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); ll l=,r=a[n-]-a[],mid;//最小距离x一定在[l,r]之间,采用二分查找方法确定x
while(l<=r)
{
mid=l+(r-l)/;
if(check(mid))//当以mid为最小距离能放下所有奶牛时,再增大mid判断能否放下
l=mid+;
else
r=mid-;
}
printf("%d\n",l- );//因为当l>r时退出while循环,所以结果l-1
}
poj 2456 Aggressive cows 贪心+二分的更多相关文章
- POJ 2456 Aggressive cows (二分)
题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
- POJ - 2456 Aggressive cows(二分+贪心)
题意:把c个牛分进n个摊位,摊位位置已知,所有摊位分布在0 <= xi <= 1,000,000,000,问两头牛间最小距离的最大值. 分析:找所有最小距离取个最大的.所以二分找这个最小的 ...
- 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 题目大意:农夫 建造了一座很长的畜栏,它包括 个隔间,这些小隔间依次编号为. 但是, 的 头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...
- 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 ...
随机推荐
- Python环境搭建-4 pip的安装和使用
pip的安装和使用 我们都知道python有很多的第三方库或者说是模块.这些库针对不同的应用,发挥不同的作用.我们在实际的项目中肯定会用到这些模块.那如何将这些模块导入到自己的项目中呢? Python ...
- IELTS Writing Task 1: two-chart answer
Thursday, January 09, 2020 The chart below shows the value of one country's exports in various categ ...
- Linux CentOS7 VMware LAMP架构Apache用户认证、域名跳转、Apache访问日志
一.Apache用户认证 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把111.com那个虚拟主机编辑成如下内容 <Virtu ...
- Python 基础之生成器
一.生成器表达式 生成器本质是迭代器,允许自定义逻辑的迭代器迭代器和生成器区别:迭代器本身是系统内置的,重写不了.而生成器是用户自定义的,可以重写迭代逻辑生成器可以用来钟方式创建: (1)生成器 ...
- sqlserver 面试题
1. --是查询A(ID,Name)表中第11至20条记录,ID作为主键可能是不是连续增长的列,完整的查询语句如下: SELECT TOP 10 * FROM dbo.Employee WHERE E ...
- Sqlserver2012 使用sql语句增加(或删除)表一个字段
前言 Mark在SqlServer 2012 的数据库使用sql语句增加(或删除)一张表的一个字段. 使用Sql语句增加表的一个字段 [1]语法: alter table table_name add ...
- java并发之Future与Callable使用
java并发之Future与Callable使用 这篇文章需要大家知道线程.线程池的知识,尤其是线程池. 有的时候我们要获取线程的执行结果,这个时候就需要用到Callable.Future.Futur ...
- IDEA配置数据库连接失败的问题
今天采用IDEA连接数据库失败了,有几个问题需要注意 首先笔者采用的数据库版本为8.0.17而IDEA自带版本是5.2.26大概,于是首先出现的问题是驱动不匹配,那么就需要换成我自己的版本,配置如下 ...
- VUE 使用axios请求第三方接口数据跨域问题解决
VUE是基于node.js,所以解决跨域问题,设置一下反向代理即可. 我这里要调用的第三方接口地址为 http://v.juhe.cn/toutiao/index?type=top&key=1 ...
- 小程序的tabar顶部和底部导航的区别
最近有人说小程序的底部tabar放在顶部会出现问题,那么先看看如何放在顶部吧:图片效果: 这里呢,在官方文档是有说明,tabbar 的属性设置里面有个position属性,position只支持bot ...