二分法的应用:最大化最小值 POJ2456 Aggressive cows
/*
二分法的应用:最大化最小值 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的更多相关文章
- 二分算法的应用——最大化最小值 POJ2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...
- 最大化最小值 Aggressive cows
Aggressive cows http://poj.org/problem?id=2456 N间小屋,M头牛,使得牛跟牛之间的距离最远,以防止牛打架. 2<=N<=100000 2< ...
- POJ2456 Aggressive cows
Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...
- POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13993 Accepted: 6775 ...
- POJ2456 Aggressive cows(二分)
链接:http://poj.org/problem?id=2456 题意:一个数轴上n个点,每个点一个整数值,有c个奶牛,要放在这些点的某几个上,求怎么放可以使任意两个奶牛间距离的最小值最大,求这个最 ...
- POJ2456 Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- poj2456 Aggressive cows(二分查找)
https://vjudge.net/problem/POJ-2456 二分,从最大长度开始,不断折半试,如果牛全放下了,就是可行,修改下界,否则改上届. #include<iostream&g ...
- POJ2456 Aggressive cows(二分+贪心)
如果C(d)为满足全部牛之间的距离都不小于d. 先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d. #include<iostream> #include<cstdio> ...
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
随机推荐
- 忘记mysql超户密码的解决方法
本文章针对用yum安装的mariadb数据库,如果是tar包安装的mysql数据库,只是数据库命令的关闭启动方式不同而已. 方法一:[root@localhost ~]# killall -u mys ...
- 数据库常用SQL语句
显示所有的数据库 show databases; 新建数据库 create database if not exists 数据库名 default character set = 'utf8'; 删 ...
- 使用navigator.userAgent来判断浏览器类型
var br=navigator.userAgent.toLowerCase(); var browserVer=(br.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ...
- node快速入门
nodejs是一个方兴未艾的技术,近几年一直活跃在各大论坛.其实nodejs也只是新瓶装旧酒,只是它的一些激进的特性使她显得很神秘.nodejs使用的是JavaScript的语法,所以在学习nodej ...
- JavaIO流之File操作
IO流: File: File(文件/文件夹地址)构造函数: public File(String path); public File(String parentPath, String child ...
- springCloud参考资料
官网: http://spring.io/projects/spring-cloud 各组件说明(中文版):https://springcloud.cc/spring-cloud-netflflix. ...
- hdu1848 Fibonacci again and again [组合游戏]
http://acm.hdu.edu.cn/showproblem.php?pid=1848 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers) ...
- date和string转换用joda包
import org.joda.time.DateTime;import org.joda.time.format.DateTimeFormat;import org.joda.time.format ...
- HTTP协议的消息头:Content-Type和Accept的作用 转载https://www.cnblogs.com/lexiaofei/p/7289436.html
一.背景知识 1.概述 Http报头分为通用报头,请求报头,响应报头和实体报头. 请求方的http报头结构:通用报头|请求报头|实体报头 响应方的http报头结构:通用报头|响应报头|实体报头 Acc ...
- jQuery 封装的ajax
jquery封装的ajax 具体操作: $.get(url [,data] [,fn回调函数] [, dataType]); data:给服务器传递的数据,请求字符串 .json对象 都可以设 ...