【题目大意】

有N间牛舍和M头牛,告诉你每个牛舍的位置,求出两头牛之间最小距离的最大值。

【思路】

二分判断两头牛之间的最小距离d,通过贪心法进行验证。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=+;
int n,m;
int pos[MAXN]; int whether(int d)
{
int now=;
int nowpos=pos[]+d;
for (int i=;i<m;i++)
{
while (pos[now]<nowpos && now<n) now++;
if (now==n)
{
return ;
}
nowpos=pos[now]+d;
}
return ;
} int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<n;i++) scanf("%d",&pos[i]);
sort(pos,pos+n); int lb=,ub=pos[n-];
while (ub-lb>)
{
int mid=(lb+ub)/;
if (whether(mid)) lb=mid;
else ub=mid;
}
cout<<lb<<endl;
return ;
}

【二分查找】POJ2456-Aggressive cows的更多相关文章

  1. 二分法的应用:最大化最小值 POJ2456 Aggressive cows

    /* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

  2. POJ2456 Aggressive cows 二分

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...

  3. 二分算法的应用——最大化最小值 POJ2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...

  4. POJ2456 Aggressive cows

    Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...

  5. POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13993   Accepted: 6775 ...

  6. poj2456 Aggressive cows(二分查找)

    https://vjudge.net/problem/POJ-2456 二分,从最大长度开始,不断折半试,如果牛全放下了,就是可行,修改下界,否则改上届. #include<iostream&g ...

  7. POJ2456 Aggressive cows(二分+贪心)

    如果C(d)为满足全部牛之间的距离都不小于d. 先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d. #include<iostream> #include<cstdio> ...

  8. POJ2456 Aggressive cows(二分)

    链接:http://poj.org/problem?id=2456 题意:一个数轴上n个点,每个点一个整数值,有c个奶牛,要放在这些点的某几个上,求怎么放可以使任意两个奶牛间距离的最小值最大,求这个最 ...

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

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

随机推荐

  1. 6.0docker Dockerfile文件

    指令格式 #注释 FROM :基础镜像 MAINTAINER:镜像的作者信息 RUN :指定(构建过程中)当前镜像中运行的命令 EXPOSE :指定运行镜像的容器应用程序所使用的端口 容器但不会打开, ...

  2. O(1)时间复杂度实现入栈、出栈、获得栈中最小元素、获得栈中最大元素(转)

    题目要求:定义栈的数据结构,添加min().max()函数(动态获取当前状态栈中的最小元素.最大元素),要求push().pop().min().max()的时间复杂度都是O(1). 思路解析:根据栈 ...

  3. C++学习之路(二):引用

    (1)引用是变量的别名 引用的基本定义格式:类型 &引用名 = 变量名 例如:int a = 1; int &b = a,这里b是a的别名,b与a都指向了同一块内存单元. 对于引用而言 ...

  4. HDU 6183 Color it 线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6183 题意: 有四种操作: 0:清除所有点 1 x y c : 给点(x, y)添加一种颜色c(颜色不 ...

  5. java数组面试题

    一维数组可以写成:int[ ]x 或者int x[ ]: 二维数组可以写成:int[ ] y [ ] 或者int y[ ][ ] 或者int [ ][ ]y 面试题如下:       声明数组int[ ...

  6. java中的i++与++i有什么区别?

    刚开始接触时,做了一些小测试,还以为这两个没有什么区别. public class OperatorDemo { public static void main(String[] args){ int ...

  7. php文件上传错误信息

    错误信息说明 UPLOAD_ERR_OK:其值为0,没有错误发生,文件上传成功 UPLOAD_ERR_INI_SIZE:其值为1,上传的文件超过了php.ini和upload_max_filesize ...

  8. hdu 2768(建图,最大点独立集)

    Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. HDR文件格式简介及其读写函数

    转自:http://blog.csdn.net/lqhbupt/article/details/7828827 1.HDR简介HDR的全称是High-DynamicRange(高动态范围).在此,我们 ...

  10. react todolist

    import React, {Component} from 'react'; class AddItem extends React.Component { constructor(props) { ...