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. PyCharm安装使用 激活码

    从pycharm官网 [http://www.jetbrains.com/pycharm/download/#section=windows]下载完整安装包 激活码激活 优点:Window.Mac.U ...

  2. Codeforces 1144G Two Merged Sequences dp

    Two Merged Sequences 感觉是个垃圾题啊, 为什么过的人这么少.. dp[ i ][ 0 ]表示处理完前 i 个, 第 i 个是递增序列序列里的元素,递减序列的最大值. dp[ i ...

  3. Python 面向对象3-类变量与实例变量

    #!/usr/bin/env python # -*- coding:utf-8 -*- # 作者:Presley # 邮箱:1209989516@qq.com # 时间:2018-08-05 # O ...

  4. ARM驱动:SEC S3C2410X Test B/D USB驱动 安装

    s3c2410x驱动是一款非常好用的usb驱动程序,win7 64位下mini2440 USB下载驱动安装,解决win7下安装SEC S3C2410X Test B/D 驱动出现叹号的问题.如果你开始 ...

  5. springboot拦截器中获取配置文件值

    package com.zhx.web.interceptor; import com.zhx.util.other.IpUtil; import org.slf4j.Logger; import o ...

  6. Largest Rectangle in a Histogram POJ - 2559 (单调栈)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  7. Nowcoder contest 370F Rinne Loves Edges (简单树形DP) || 【最大流】(模板)

    <题目链接> 题目大意: 一个 $n$ 个节点 $m$ 条边的无向连通图,每条边有一个边权 $w_i$.现在她想玩一个游戏:选取一个 “重要点” S,然后选择性删除一些边,使得原图中所有除 ...

  8. Codeforces 1105C Ayoub and Lost Array (计数DP)

    <题目链接> 题目大意: 有一个长度为 n 的数列的未知数列,数列的每一个数的值都在区间 [l,r]  的范围内.现在问你能够构成多少个这样的数组,使得数组内的所有数的和能够被 3 整除. ...

  9. HDU3339 In Action 【最短路】+【01背包】

    <题目链接> 题目大意: 给出一个0-n组成的图,1-n的点上分布着值为pow的电站,给出图的m条边以及距离,从0出发到n个点中的x个点的行走距离和最小(因为是每炸一个点派出一辆坦克),且 ...

  10. Django模板之通用模板的使用

    Django模板之通用模板的使用 转载:https://code.ziqiangxuetang.com/django/django-template.html 我们做网站有一些通用的部分,比如 导航, ...