/*
二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18125 Accepted: 8636
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.
Source USACO 2005 February Gold
*/ import java.util.Arrays;
import java.util.Scanner; public class Main {
static int N, M;
static int[] a; public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
a = new int[N];
for (int i = 0; i < N; i++)
a[i] = sc.nextInt();
sc.close();
Arrays.sort(a);
int low = -1, high = 1000000001;
while (high - low > 1) {
int mid = (low + high) / 2;
if (C(mid)) {
low = mid;
} else {
high = mid;
}
}
System.out.println(low);
} static boolean C(int X) {
int last = 0;
for (int i = 1; i < M; i++) {
int next = last + 1;
while (next < N && a[next] - a[last] < X) {
next++;
}
if (next >= N) return false;
last = next;
}
return true;
}
}

二分法的应用:最大化最小值 POJ2456 Aggressive cows的更多相关文章

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

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

  2. 最大化最小值 Aggressive cows

    Aggressive cows http://poj.org/problem?id=2456 N间小屋,M头牛,使得牛跟牛之间的距离最远,以防止牛打架. 2<=N<=100000 2< ...

  3. POJ2456 Aggressive cows

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

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

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

  5. POJ2456 Aggressive cows(二分)

    链接:http://poj.org/problem?id=2456 题意:一个数轴上n个点,每个点一个整数值,有c个奶牛,要放在这些点的某几个上,求怎么放可以使任意两个奶牛间距离的最小值最大,求这个最 ...

  6. POJ2456 Aggressive cows 二分

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

  7. poj2456 Aggressive cows(二分查找)

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

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

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

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

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

随机推荐

  1. 关于C#中Convert.ToInt32()是干什么用的

    并非每个对象都可以直接转换为int.例如,以下内容将无法编译: string Maomao = "100"; int i = (int)Maomao; 因为string无法隐式转换 ...

  2. Vue学习笔记【4】——Vue指令之v-on

    Vue指令之v-on v-on指令介绍 直接使用指令v-on 使用简化指令@ 绑定事件代码:@事件名="methods中的方法名称" <!DOCTYPE html> & ...

  3. Java——类之间的关系

    3.7 类之间的关系 3.7.1 泛化关系 类和类之间的继承关系及接口与接口之间的继承关系. 3.7.2 实现关系 类对接口的实现. 3.7.3 关联关系 类与类之间的连接,一个类可以知道另一个类的属 ...

  4. SpringDataRedis依赖

    <dependencies>        <dependency>            <groupId>junit</groupId>       ...

  5. 在IntelliJ IDEA中新建Maven项目

    在IntelliJ IDEA中新建Maven项目,选择“File->New->Project”,创建一个简单项目,不选择模板,如下图所示: 2 选择“Maven”,不需要使用内置结构(模板 ...

  6. 【LeetCode 20】有效的括号

    题目链接 [题解] 一道傻逼括号匹配题 [代码] class Solution { public: bool isValid(string s) { vector<char> v; int ...

  7. bzoj1098题解

    [题意分析] 给你一张无向图,求其补图的联通块数及各个联通块大小. [解题思路] 暴搜! 然而n2会T怎么办? 仔细观察发现m远小于n2,也就是说这是一张极其稠密的补图. 这时就要用到黑科技了:flo ...

  8. 管理员技术(七): Linux管理员 综合测试

    一.Linux管理员 综合测试 目标: 根据本文提供的练习步骤完成所有练习案例. 方案: 开始练习之前,先依次重置虚拟机环境. [root@room9pc13 ~]# rht-vmctl  reset ...

  9. PHP base64_decode+gzinflate压缩和解密代码图文教程

    今天碰到的这个问题,是我在更换一个WP主题是遇到.目前的情况如下,我想要把如下的超链接去掉,后台代码找到了在sidebar1.php文件中. 打开此文件发现是一长串的字符: 经过百度.google后, ...

  10. C++语言编程基础

    C++程序设计语言可以看作C语言的改进和升级,不仅完全兼容了C语言的语法和函数库,还引入了面向对象.运算符重载.多态性.数据流和模板等最新的编程思想.极大地保证了源代码的生产率.质量和可重用性.GNU ...