二分法的应用:最大化最小值 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/ ...
随机推荐
- mongoose 常用数据库操作 更新
更新 Model.update(conditions, update, [options], [callback]) db.js var mongoose = require('mongoose'); ...
- 高性能js之js文件的加载与解析
随着网站的发展,现在的网页已经离不开js,经常一个页面会引入大量的js.那么该如何合理的加载这些js? head标签中引入js文件可能是最常见的一种方式,但是这样会造成一个问题.因为j可以说是浏览器中 ...
- js过滤字符串中的html标签
var str = 'add<a>daad</a><p>fsdada</p>' str.replace(/<[^<>]+>/g, ...
- 获取min-max之间的随机数
private static String getRandom(int min, int max){ Integer random =(int)(min+Math.random()*(max-min+ ...
- Nginx缓存配置指南
1.如何配置基本缓存设置 开启简单的缓存配置,只需要两个指令:proxy_cache_path和proxy_cache.proxy_cache_path配置缓存的存放地址和其他的一些常用配置,prox ...
- 【纪中集训】2019.07.11【NOIP提高组】模拟 B 组TJ
Preface 今天的B组题确实比A组难多了... T1 Description 有一个长为\(n(n\in[1,2*10^5])\)的01串,有\(m(m\in[1,10^5])\)个限制\(a_i ...
- ZROI week1
\[ZROI day1\] \[Grid\] 题目描述 给定一个矩阵,小写字母,求一条路径使得从\((1,1) -> (n,m)\),字典序最小,并且每次只能向右或者向下. 题解 先考虑如果没有 ...
- STM32嵌入式开发学习笔记(七):串口通信(下)
下面我们进行几个串口通信的实际应用. 实验一:发信实验,让开发板通过串口向电脑发送信息: #include <stdio.h> #include <stm32f10x.h> # ...
- shell重定向的顺序问题
三个默认的文件描述符 0: stdin(标准输入) 1: stdout(标准输出) 2: stderr(标准错误输出) 系统中这3个文件描述符所对应的文件: 重定向顺序 示例脚本 echo " ...
- Java-Class-@I:javax.annotation.PostConstruct
ylbtech-Java-Class-@I:javax.annotation.PostConstruct 1.返回顶部 2.返回顶部 1.1. package com.ylbtech.api.pl ...