【题目大意】

有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. O(1)时间复杂度实现入栈、出栈、获得栈中最小元素、获得栈中最大元素(转)

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

  2. perl6中字符串字母编历

    use v6; my $input = prompt '输入字符串:'; for $input.words -> $word { say $word; } for $input.comb -&g ...

  3. 2018DDCTF Misc部分WP

    题目给出了这样一串字符:d4e8e1f4a0f7e1f3a0e6e1f3f4a1a0d4e8e5a0e6ece1e7a0e9f3baa0c4c4c3d4c6fbb9e1e6b3e3b9e4b3b7b7 ...

  4. bisai2.py

    比赛专用py #!/usr/bin/env python #encoding:utf- #by i3ekr import re,requests,os res = "(flag{.*?}&q ...

  5. 【Linux学习】nohup后台运行程序以及输出重定向

    Linux有两种命令使程序后台运行 第一种:支持后台运行,但是关闭终端的话,程序也会停止 command & 第二种:支持后台运行,关闭终端后,程序也会继续运行 nohup command & ...

  6. 命令行创建KVM虚拟机

    qemu命令创建虚拟机: qemu-img create -f qcow2 /home/ubuntu.img 20G   qemu-system-x86_64 -m 2048 -enable-kvm ...

  7. 多线程伪共享FalseSharing

    1. 伪共享产生: 在SMP架构的系统中,每个CPU核心都有自己的cache,当多个线程在不同的核心上,并且某线程修改了在同一个cache line中的数据时,由于cache一致性原则,其他核心cac ...

  8. freemark基础知识

    前言:使用freemarker对应生成一个html文件,保存到磁盘,访问文件就不一定使用tomcat,可以使用nginx(http服务器)访问.可以使用freemaker工具生成.只生成一次,html ...

  9. 如何设置static tableview的section区域高度

    重写代理方法- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { i ...

  10. 80端口被System进程占用问题

    更新: 有可能占用80端口的服务: 如果安装了IIS,关闭IIS: 如果未开启IIS功能,而安装了诸如Web Matrix的开发程序,则有可能被Web Development Agent Servic ...