/*
二分法的应用:最大化最小值 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. element-UI select 踩过的坑和解决办法

    今天遇到了一个bug,就是在使用element-UI的select框时,当选中值,会触发@change事件,下拉框消失,但是这时候select框还是处于获取焦点状态.可以看到select框还是处于颜色 ...

  2. anyka安凯微电子

    http://www.anyka.com/   幼教机

  3. kindeditor加入方法

    1.editor文件夹拷进来 2. editor里jsp子文件夹里找到几个jar拷贝到网站的web-app里的lib下 3.  网页里 head里加个这个 <link rel="sty ...

  4. 【Nacos】本地集群部署

    关于Nacos已经展开了四篇入门文章: 初探Nacos(一)-- 单机模式启动 初探Nacos(二)-- SpringCloud使用Nacos的服务注册与发现 初探Nacos(三)-- SpringB ...

  5. jQuery, js 验证两次输了密码的一相同

    <div class="form-group"> <label class="col-sm-2 control-label font"> ...

  6. .net 实用小技巧

    Linq 实现in查询 Dictionary<int, IP> dict = new Dictionary<int, IP>(); dict.Add(1, new IP(&qu ...

  7. linux:lrzsz安装

    Linux中的lrzsc是linux里可代替ftp上传和下载的程序. yum install lrzsc 没有可用软件包 lrzsc. 这时使用 -y即可安装 centos安装:yum -y inst ...

  8. IIS身份验证和文件操作权限(二、匿名身份验证)

    一.配置匿名身份验证 二.浏览站点 -- 操作文件 ①无操作权限 点击写入 ②有操作权限(IIS_IUSRS.Authenticated Users两个任选一个) 点击写入

  9. 互联网大厂高频重点面试题 (第2季)JUC多线程及高并发

    本期内容包括 JUC多线程并发.JVM和GC等目前大厂笔试中会考.面试中会问.工作中会用的高频难点知识.斩offer.拿高薪.跳槽神器,对标阿里P6的<尚硅谷_互联网大厂高频重点面试题(第2季) ...

  10. 1029 Median (25 分)

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...