Aggressive cows

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25944   Accepted: 11982

Description

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000).

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

* Line 1: Two space-separated integers: N and C

* Lines 2..N+1: Line i+1 contains an integer stall location, xi

Output

* Line 1: One integer: the largest minimum distance

Sample Input

5 3
1
2
8
4
9

Sample Output

3

Hint

OUTPUT DETAILS:

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.

 
题意:有n个位置,m头奶牛,将m头奶牛放到n个位置中,在每一种放置方法中,都有一个两头奶牛之间间隔的最小距离,求在所有放置方法中,这个最小距离的最大值
 
题解:先确定最小距离的范围是在[ 0 , a[n-1]-a[0] ]之间,用二分查找不断逼近可以放下所有奶牛的最大值
 
#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 贪心+二分的更多相关文章

  1. POJ 2456 Aggressive cows (二分)

    题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...

  2. [ACM] poj 2456 Aggressive cows (二分查找)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 D ...

  3. POJ - 2456 Aggressive cows(二分+贪心)

    题意:把c个牛分进n个摊位,摊位位置已知,所有摊位分布在0 <= xi <= 1,000,000,000,问两头牛间最小距离的最大值. 分析:找所有最小距离取个最大的.所以二分找这个最小的 ...

  4. poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分

    poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...

  5. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

  6. POJ 2456 Aggressive cows(贪心 + 二分)

    原题链接:Aggressive cows 题目大意:农夫 建造了一座很长的畜栏,它包括  个隔间,这些小隔间依次编号为. 但是, 的  头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...

  7. POJ 2456 Aggressive cows (二分 基础)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7924   Accepted: 3959 D ...

  8. POJ 2456 Aggressive cows(二分答案)

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...

  9. POJ - 2456 Aggressive cows 二分 最大化最小值

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18099   Accepted: 8619 ...

随机推荐

  1. Jquery 获取控件的值

    1:通过控件的ID 获取值 $("input[name='weiKuanDate']").val(); 2:通过控件的name 获取值 $("input[name='we ...

  2. 安装mysql过程中的异常解决

      [root@cdh1 ruanjian]# rpm -ivh mysql-community-common-5.7.10-1.el6.x86_64.rpm  warning: mysql-comm ...

  3. ImagePullBackOff 错误处理

    kubectl create -f nginx-deployment.yaml 执行之后,kubectl get pods 一致出现 ImagePullBackOff,一直无法执行成功. yaml文件 ...

  4. vue中配置sass(包含vue-cli 3)

    目录 vue vue cli 3 老版本的脚手架搭建的项目 版本 安装 不用修改任何配置 vue文件中使用 vue 更新时间: 2018-09-21 vue cli 3 选择 Manually sel ...

  5. 利用django打造自己的工作流平台(一):从EXCEL到流程化运作

    因工作所需以及管理个人一些日常事项,自己基于django(一个基于python的web框架,详细介绍可查阅相关资料)开发了一个简易的工作流平台[平台地址].本文首先简要介绍工作流平台的设计思想及其在项 ...

  6. 1.requests+正则表达式爬猫眼电影TOP100

    import requests from requests.exceptions import RequestException def get_one_page(url):try: response ...

  7. mysql 优化策略(如何利用好索引)

    博客园 首页 联系 管理 随笔- 3282  文章- 0  评论- 157  mysql联合索引 命名规则:表名_字段名1.需要加索引的字段,要在where条件中2.数据量少的字段不需要加索引3.如果 ...

  8. phpcms安装与使用

    安装完wamp(或phpstudy)之后,将phpcms的安装文件复制入C:\wamp\www(或C:\phpStudy\PHPTutorial\WWW)文件夹下: 打开phpcms_v9_UTF8\ ...

  9. 前端Cannot read property 'disabled' of null 问题解决

    就是在项目中,控制台一直在报这个错,一直没找到是什么问题, 后来经过一番排查,发现是 因为在页面中使用了el-dropdown,但是在这个标签里面没有设置它的子元素,所以会报错,解决的方法就是在el- ...

  10. Thymeleaf--起步

    Spring官方支持的服务的渲染模板中,并不包含jsp.而是Thymeleaf和Freemarker等,而Thymeleaf与SpringMVC的视图技术,及SpringBoot的自动化配置集成非常完 ...