Aggressive cows

总时间限制: 1000ms 内存限制: 65536kB

描述

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?

输入

* Line 1: Two space-separated integers: N and C

* Lines 2..N+1: Line i+1 contains an integer stall location, xi

输出

* Line 1: One integer: the largest minimum distance

样例输入

5 3
1
2
8
4
9

样例输出

3

提示

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.

来源

USACO 2005 February Gold

题目OJ链接:http://bailian.openjudge.cn/practice/2456/

题目分析:

(参考http://blog.csdn.net/wuxiushu/article/details/49158843)

题意要表达的是:把C头牛放到N个带有编号的隔间里,使得任意两头牛所在的隔间编号的最小差值最大。例如样例排完序后变成1 2 4 8 9,那么1位置放一头牛,4位置放一头牛,它们的差值为3;最后一头牛放在8或9位置都可以,和4位置的差值分别为4、5,和1位置的差值分别为7和8,不比3小,所以最大的最小值为3。

分析:这是一个最小值最大化的问题。先对隔间编号从小到大排序,则最大距离不会超过两端的两头牛之间的差值,最小值为0。所以我们可以通过二分枚举最小值来求。假设当前的最小值为x,如果判断出最小差值为x时可以放下C头牛,就先让x变大再判断;如果放不下,说明当前的x太大了,就先让x变小然后再进行判断。直到求出一个最大的x就是最终的答案。

本题的关键就在于讨论差值的大小。

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
const int MAX = 100010;
int a[MAX],n,m;

bool C(int d)
{
    int t = a[0],count = 1;
    for(int i = 1;i < n;i ++)
    {
        if(a[i] - t >= d)
        {
            count ++;
            t=a[i];
            if(count >= m)
                return true;
        }
    }
    return false;
}

int solve()
{
    int x = 0,y = a[n-1] - a[0];
    while(x <= y)
    {
        int mid=(x+y)/2;
        if(C(mid))
            x=mid + 1;
        else
            y=mid - 1;
    }
    return x - 1;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i = 0;i < n;i ++)
        scanf("%d",&a[i]);
        sort(a,a+n);
        printf("%d\n",solve());
    }
    return 0;
}

原文地址:https://www.cnblogs.com/huashanqingzhu/p/7617609.html

Aggressive cows相关推荐

  1. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 1 /* 2 二分搜索:搜索安排最近牛的距离不小于d 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #incl ...

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

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

  3. BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )

    最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...

  4. 二分入门——poj 2456 aggressive cows

    这是一道二分的神奇贪心题,先上题目 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11714 A ...

  5. Aggressive cows POJ2456

    Aggressive cows(愤怒的牛)(分治) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40455 Accepted: ...

  6. Aggressive cows POJ - 2456

    2456 -- Aggressive cows 题目大意:有c只牛,要把他们放在n个屋子里,每个屋子放一只牛,每个屋子都在一根数轴上,给出他们的坐标,要让每个屋子间的最小距离尽可能的大,问这个最大距离 ...

  7. POJ 2456 - Aggressive cows(二分)

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

  8. Aggressive cows(c语言)

    题目描述 Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are ...

  9. poj 2456 Aggressive cows 【二分+最大化最小值】

    题目链接:http://poj.org/problem?id=2456 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 210 ...

  10. poj2456 Aggressive cows(二分查找)

    https://vjudge.net/problem/POJ-2456 二分,从最大长度开始,不断折半试,如果牛全放下了,就是可行,修改下界,否则改上届. 1 #include<iostream ...

最新文章

  1. SharePoint2010-CustomAction中的ContentTypeId属性已经消失了
  2. 003_ZooKeeper服务端集群搭建
  3. set class_ x set fid_ x
  4. python数据清洗实例_python 数据的清理行为实例详解
  5. 使用EasyUI Tree异步加载JSON数据 生成树
  6. iOS开发之本地通知UILocalNotification
  7. 安卓使用jdbc连接mysql_Android:使用JDBC连接MySql
  8. AnswerOpenCV(1001-1007)一周佳作欣赏
  9. 火狐ok谷歌适配_“ OK Google”在锁定手机上的安全性越来越高
  10. heartbeat与keepalived对比
  11. 官方 STM32F303ZE Nucleo-144开发板 按键使用
  12. Cocos2d-js-v3.2 在 mac 上配置环境以及编译到 Andorid 的注意事项(转)
  13. 2143亿!2018年天猫“双11”成交总额是这样预测的
  14. 推理集 —— 特殊的空间
  15. Vim 和 NeoVim 曝出高危漏洞
  16. 优秀工程师应该具备哪些素质_工程师应具备的素养有哪些
  17. 【保姆级|建议收藏】阿ken带你学Java入门及进阶(二)——运算符、循环语句,文末有彩蛋鸭✨✨✨
  18. 卸载360企业版密码
  19. 第三周总结(2022.10.31~2022.11.4)
  20. 开发中常见的算法汇总之-基数排序

热门文章

  1. QQ邮箱中转站文件即将过期时如何转存到微云
  2. 2021年教你如何用笔记本把Windows10系统安装到移动固态硬盘或U盘简易教程
  3. php后门文件,php后门屌炸天
  4. Python 查询全年天数
  5. 深度系统linux deepin如何按装,安装深度Deepin 15.11操作系统的方法
  6. dpdk bond口学习笔记
  7. pcap报文80211-8023区别
  8. 关于“应用程序正常初始化(0xc0150002)失败”问题的解决方案
  9. gatk过滤_GATK使用方法详解(相关参数和参考文件说明)
  10. 中证500指数的跟踪基金最近1年收益排名