https://vjudge.net/problem/POJ-2456

二分,从最大长度开始,不断折半试,如果牛全放下了,就是可行,修改下界,否则改上届。

 #include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define INF 0x3f3f3f3f
typedef unsigned long long ll;
using namespace std;
int n, m, a[];
int C(int x)
{
int pre=, ans=;
for(int i = ; i < n; i++){
while(i < n&&a[i]-a[pre]<x){
i++;
}
if(i>=n) break;
ans++;
pre = i;
}
return ans >= m;
}
int main()
{
while(~scanf("%d%d", &n, &m)){
for(int i = ; i < n; i++){
scanf("%d", &a[i]);
}
sort(a, a+n);
int lb = , ub = INF;
while(ub-lb>){
int mid = (ub+lb)/;
if(C(mid)){
lb = mid;
}
else ub = mid;
}
printf("%d\n", lb);
}
return ;
}

poj2456 Aggressive cows(二分查找)的更多相关文章

  1. POJ2456 Aggressive cows 二分

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

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

    题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...

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

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

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

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

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

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

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

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

  7. POJ2456 Aggressive cows

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

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

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

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

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

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

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

随机推荐

  1. Ubuntu安装搜狗sougou输入法

    昨天ubuntu闪屏后进不去系统,之后重新安装的Ubuntu14.04,在软件中心安装了新立得软件包管理器 在网上下载搜狗输入法forlinux 网址:http://pinyin.sogou.com/ ...

  2. OAuth2:隐式授权(Implicit Grant)类型的开放授权

    适用范围 仅需临时访问的场景 用户会定期在API提供者那里进行登录 OAuth客户端运行在浏览器中(Javascript.Flash等) 浏览器绝对可信,因为该类型可能会将访问令牌泄露给恶意用户或应用 ...

  3. Rookey.Frame企业级极速开发框架

    项目详细介绍 Rookey.Frame是一套基于.NET MVC + easyui的企业级极速开发框架,支持简单逻辑模块零代码编程.支持工作流(BPM).支持二次开发,具有高扩展性.高复用性.高伸缩性 ...

  4. vnc启动失败

    SSH登陆服务器,用命令行重启VNC服务 $service vncserver start 提示如下错误: Starting VNC server: no displays configured    ...

  5. Python select解析

    一.首先列一下,sellect.poll.epoll三者的区别 1.select a.select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监控多个文件描述符的数组,当 ...

  6. CSS改变插入光标颜色caret-color

    CSS代码: input { color: #333; caret-color: red; } @supports (-webkit-mask: none) and (not (caret-color ...

  7. Thinkphp3.1 php 链接SqlServer

    ThinkPHP链接 M("lk_employeeInfo","Null/表前缀","sqlsrv://账号:密码@服务器:端口/数据库") ...

  8. Codeforces Gym100543L Outer space invaders 区间dp 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF-Gym100543L.html 题目传送门 - CF-Gym100543L 题意 $T$ 组数据. 有 $n ...

  9. CodeForces 516C Drazil and Park 线段树

    原文链接http://www.cnblogs.com/zhouzhendong/p/8990745.html 题目传送门 - CodeForces 516C 题意 在一个环上,有$n$棵树. 给出每一 ...

  10. sql 跨服务器查询

    创建链接服务器 exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' exec sp_addlinkedsrvlogin ...