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. 【剑指Offer面试编程题】题目1360:乐透之猜数游戏--九度OJ

    题目描述: 六一儿童节到了,YZ买了很多丰厚的礼品,准备奖励给JOBDU里辛劳的员工.为了增添一点趣味性,他还准备了一些不同类型的骰子,打算以掷骰子猜数字的方式发放奖品.例如,有的骰子有6个点数(点数 ...

  2. 【原】nginx配置文件

    一:下载nginx方式 1.yum install nginx 2.源码安装 二:学习网址 nginx documentation — DevDocs 三:配置文件信息 server { listen ...

  3. 如何启动mac版docker自带的k8s

    最近准备好好学习下k8s,为了图方便,直接使用docker集成的k8s,但是网上找了一些教程但都没能一次性成功,只好自己从头跑一遍,顺手写个教程可以方便有类似需求的同学参考. 话不多说,直接上步骤. ...

  4. mysql遇到java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed

    在连接数据库的url中,加上allowPublicKeyRetrieval=true from:https://blog.csdn.net/Gushiyuta/article/details/9323 ...

  5. stringstream常见用法介绍

    1 概述 <sstream> 定义了三个类:istringstream.ostringstream 和 stringstream,分别用来进行流的输入.输出和输入输出操作.本文以 stri ...

  6. Flutter 使用json_model解析json生成dart文件

    一.json_serializable使用步骤 1.集成json_serializable pubspec.yaml 添加以下依赖 dependencies: json_annotation: ^2. ...

  7. Pytorch【直播】2019 年县域农业大脑AI挑战赛---初级准备(一)切图

    比赛地址:https://tianchi.aliyun.com/competition/entrance/231717/introduction 这次比赛给的图非常大5万x5万,在训练之前必须要进行数 ...

  8. redis api-String

  9. Q12问题

    1.我知道公司对我的工作要求吗? 2.我有做好我的工作所需要的材料和设备吗? 3.在工作中,我每天都有机会做我最擅长做的事吗? 4.在过去的七天里,我因工作出色而受到表扬吗? 5.我觉得我的主管或同事 ...

  10. @Controller 和 @RestController 的区别

    @Controller和@RestController的区别? 官方文档:@RestController is a stereotype annotation that combines @Respo ...