• 原题如下:

    Aggressive cows
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 20524   Accepted: 9740

    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(d):=可以安排牛的位置使得最近的两头牛的距离不小于d,那么问题就变成了求满足C(d)的最大的d。另外,最近的间距不小于d也可以说成是所有牛的间距都不小于d,因此就有C(d)=可以安排牛的位置使得任意的牛的间距都不小于d,这个问题的判断使用贪心法就可以解决。
  • 代码:
     #include <cstdio>
    #include <cctype>
    #include <algorithm>
    #include <cmath>
    #define num s-'0' using namespace std; const int MAX_N=;
    const int INF=0x3f3f3f3f;
    int N,M;
    int x[MAX_N]; void read(int &x){
    char s;
    x=;
    bool flag=;
    while(!isdigit(s=getchar()))
    (s=='-')&&(flag=true);
    for(x=num;isdigit(s=getchar());x=x*+num);
    (flag)&&(x=-x);
    } void write(int x)
    {
    if(x<)
    {
    putchar('-');
    x=-x;
    }
    if(x>)
    write(x/);
    putchar(x%+'');
    } bool C(int); int main()
    {
    read(N);read(M);
    for (int i=; i<N; i++) read(x[i]);
    sort(x, x+N);
    int lb=, ub=INF;
    while (ub-lb>)
    {
    int mid=(lb+ub)/;
    if (C(mid)) lb=mid;
    else ub=mid;
    }
    write(lb);
    putchar('\n');
    } bool C(int d)
    {
    int last=;
    for (int i=; i<M; i++)
    {
    int crt=last+;
    while (crt<N && x[crt]-x[last]<d) crt++;
    if (crt==N) return false;
    last=crt;
    }
    return true;
    }

Aggressive cows(POJ 2456)的更多相关文章

  1. Divide and conquer:Aggressive Cows(POJ 2456)

    侵略性的牛 题目大意:C头牛最大化他们的最短距离 常规题,二分法即可 #include <iostream> #include <algorithm> #include < ...

  2. 【算法】题目分析:Aggressive Cow (POJ 2456)

    题目信息 作者:不详 链接:http://poj.org/problem?id=2456 来源:PKU JudgeOnline Aggressive cows[1] Time Limit: 1000M ...

  3. poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分

    poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...

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

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

  5. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

  6. POJ 2456 Aggressive cows (二分)

    题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...

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

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

  8. POJ 2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11192   Accepted: 5492 ...

  9. POJ 2456: Aggressive cows(二分,贪心)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20485   Accepted: 9719 ...

随机推荐

  1. Css 设置超过再两行显示省略号

    大部分场景都是超过一行就显示... <template> <div class="other-product-item item-name" :title=&qu ...

  2. asp.netcore 3.1 program、Startup 类详解

    Program类 public class Program { /// <summary> /// 应用程序入口 /// 1.asp.netcore 本质上是控制台程序 /// </ ...

  3. Virtuoso 中如何优化 Library Manager 的显示

    https://www.cnblogs.com/yeungchie/ 主要从 cds.lib 文件去入手. 假设现在想把 Virtuoso 预装的库整理到 preload 分类(库)中. 首先创建一个 ...

  4. PMI-ACP认证,你了解多少?

    PMI-ACP ®(敏捷管理专业人士)认证是由美国项目管理协会(PMI ®)于2011年推出的一门敏捷项目管理考试科目,全称Agile Certified Practitioner.PMI-ACP®证 ...

  5. Socket原理及实践(Java/C/C++)

    原理 基本概念 什么是TCP/IP.UDP? 详细的可以看一下这个:学习计算机网络知识只要一篇就够了! TCP/IP(Transmission Control Protocol/Internet Pr ...

  6. 「查缺补漏」巩固你的RocketMQ知识体系

    Windows安装部署 下载 地址:[https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.5.2/rocketmq-all-4.5.2-bin- ...

  7. golang并发

    1.goroutine goroutine是Go并行设计的核心.goroutine说到底其实就是线程,但是它比线程更小,十几个goroutine可能体现在底层就是五六个线程,Go语言内部帮你实现了这些 ...

  8. MYSQL语法(一)

    数据表准备: CREATE TABLE student3 ( id int, name varchar(20), age int, sex varchar(5), address varchar(10 ...

  9. golang 判断IPv4 or IPv6 address

    import strings func IsIPv4(address string) bool { return strings.Count(address, ":") < ...

  10. Vue源码分析之实现一个简易版的Vue

    目标 参考 https://cn.vuejs.org/v2/guide/reactivity.html 使用 Typescript 编写简易版的 vue 实现数据的响应式和基本的视图渲染,以及双向绑定 ...