• 原题如下:

    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. C#算法设计之知识储备

    前言 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/669 访问. 算法的讨论具有一定的规则,其中也包含一些不成文的约定 ...

  2. Linux 实现密钥免密登录

    Linux 实现密钥免密登录 环境:CentOS 7 一. 创建密钥 登录Linux,进入/root/.ssh/目录下 执行ssh-keygen -t rsa创建密钥一直回车即可,因为是免密所以直接回 ...

  3. 在服务器上使用python-gym出现的显示问题

    参考链接: http://www.luyixian.cn/news_show_392045.aspx https://www.cnblogs.com/cenariusxz/p/12666938.htm ...

  4. pandas 数据类型转换及描述统计

    处理数据的时候往往需要对原始数据进行类型转换和预览等操作,下面介绍常用的处理预览和数据转换方法 预览:例: import pandas as pdsec_weather = pd.read_table ...

  5. Next Cloud通过修改数据库表,达到替换文件而不改变分享的链接地址的效果,以及自定义分享链接地址

    Next Cloud如何通过修改数据库表,达到替换文件而不改变分享的链接地址的效果,以及自定义分享的链接地址 本文首发于我的个人博客:https://chens.life/nextcloud-chan ...

  6. java项目的心得,java项目的代码层次的架构划分

    java项目使用的架构是ssm(Spring+SpringMVC+MyBatis). 一.后台代码一般分三层,Controller,Service,Dao. 1.Controller层是对前端或者接口 ...

  7. java23种设计模式—— 二、单例模式

    源码在我的github和gitee中获取 介绍 单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. ...

  8. Java后台服务慢优化杂谈

    Java后台服务慢优化杂谈 前言 你是否遇到过这样的场景,当我们点击页面某个按钮后,页面一直loading,要等待好几分钟才出结果的画面,有时直接502或504,作为一个后台开发,看到自己开发的系统是 ...

  9. Xposed+X5使用

    1.Xposed 2.X5

  10. guzzle下载图片(laravel+vue)

    先再laravel安装guzzle扩展包:composer require guzzlehttp/guzzle 之后再控制器操作: use GuzzleHttp\Client; //远程api数据的获 ...