首页 > 其他分享 >二分查找水题--疯牛(POJ 2456)

二分查找水题--疯牛(POJ 2456)

时间:2023-02-17 15:01:09浏览次数:40  
标签:right return 头牛 水题 -- 2456 mid int left


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.

本题表达的是 , 把 C 头牛 放进N 个带编号的牛舍里(隔间),使得任意两个牛之间的距离(位置的最小差值)最大. 比如样本输入,

将3 头牛 放入 1 ,4 ,8 中 , 任意两头牛的距离 是 3 , 4 , 7 . 没有比 3 还要大的最小距离 , 不信, 比如 1 8 9 ,距离 有 7 1 8 ,最小值为1 比3 小 ,.... 不举例了 , 假设任意两个牛之间距离都大于3 , 考虑贪心 ,按隔间大小从小到大排序 ,第一头牛放入 1 号牛舍 ,则至少需要放进 8 号的牛舍 , 此时第三天牛只能进 9号了 , 9-8 =1 >3 ,与假设矛盾

   简化问题, 判断最小距离为X时是否可放下 C头牛 , 那么目标就转化成了,从小到大枚举 X , 判断是否可行 ,直到找到第一个可行的X ,这就是答案 . 为了加快算法速度 ,可使用二分查找代替顺序枚举 ,

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std ;

/* 疯牛问题
最小距离最大化;
*/
const int MAX =100005 ;
int n , c ;
int L[MAX] ;
bool C(int x )
{
int i ;
int temp = L[0] ;
int count = 1 ;
for(i = 1 ; i < n ; i++)
{
if(L[i]-temp >=x)
{
temp = L[i] ;
count ++ ;
if(count >= c)
return true ;
}

}

return false ;
}
int Binary_Search()
{
int left = 0 ;
int right = L[n-1] - L[0] ;
while(right >= left )
{
int mid = left + (right - left )/ 2 ;
if(C(mid))
{
left = mid + 1 ;
}
else
{
right = mid - 1 ;
}

}

return left -1 ;

}
int main()
{
int i ;
while (scanf("%d%d",&n,&c)!=EOF)
{
for ( i= 0 ; i<n ; i++)
{
scanf("%d",&L[i]);
}
sort(L,L+n);
printf("%d\n",Binary_Search());

}


return 0 ;
}

 

 

标签:right,return,头牛,水题,--,2456,mid,int,left
From: https://blog.51cto.com/u_15970235/6064121

相关文章

  • 快速找到和为0的四个数
    DescriptionTheSUMproblemcanbeformulatedasfollows:givenfourlistsA,B,C,Dofintegervalues,computehowmanyquadruplet(a,b,c,d)∈AxBxCx......
  • 跳马问题
    题目:洛谷P1644跳马问题题目背景在爱与愁的故事第一弹第三章出来前先练练四道基本的回溯/搜索题吧……题目描述中国象棋半张棋盘如图1所示。马自左下角(0,0)向右上角(m,n)跳。......
  • 【牛客网】字符串的最后一个单词的长度
    题目描述计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)输入描述:输入一行,代表要计算的字符串,非空,长度小于5000。输出......
  • mysql:分组查询每组最新的一条数据
    我们经常遇到类似这样的需求,查询最近N秒、N分钟、N小时的数据及N天的数据,相关的方法和函数很多,本人最近用的MySQL数据库,也就用MySQL为例,大概介绍几种比较通用的方法。一、......
  • ffprobe简介
    ffprobe是一个开源的多媒体分析工具,是FFmpeg多媒体框架的一部分,可以用于检测和分析音频和视频文件。它可以读取各种视频和音频容器格式(例如MP4,AVI,MKV,FLV等)以及编码格......
  • C语言填空:满足指定输出结果
    #include<stdio.h>//补充程序使输出结果是1,2,6,24,120【1】ff(intn){【2】intf=1;f=【3】;returnf;}main(){inti;for(i=1;【4】......
  • Vue生命周期
    Vue生命周期Vue生命周期简介从Vue实例创建开始,到实例被销毁,是一个Vue实例的生命周期,总共经历了8个钩子函数。Vue的生命周期的过程称之为面向切面编程(AOP),实际上这在djang......
  • regex ^ and $
    Differencebetween^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,15}$ vs(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,15} demonstratethedifferencethroughexamples.Thedif......
  • odoo原生form表单改造成输入框
    引子:odoo作为快速搭建网站的框架,我们在利用它便捷高效功能的同时,有没有觉得这个页面,不太好看呢?今天我们一起来聊聊如果让odoo原生的form表单更美观更符合用户体验~ Od......
  • Power BI 刷新数据集,提示报错:Return records size cannot exceed 83886080
    错误效果:  错误原因:Dataverse链接方式,有80M的容量限制处理办法:更换PowerBI里面的数据源链接方式1、使用官方连接方式加载具体连接方式参照官网文档:将PowerB......