/*
二分法的应用:最大化最小值 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. 群晖修改启用root账号密码

    DSM6.0以后,官方修改了系统的ROOT密码;需要修改才能启用并使用 软件准备 PUTTY点击下载 DSM中开启SSH 控制面板-终端机和SNMP-启动SSH 打开PUTTY 输入DSM IP地址 ...

  2. Linear Regression(一)——

    Linear Regression(一)-- 机器学习 回归 定义 回归的定义 在平面上存在这些点我希望能用一条直线尽可能经过它们. 于是我们画了下面的一条直线 这样的过程就叫做回归. 这个过程中我们 ...

  3. 微信小程序分享功能的path路径

    表示被微信小程序坑惨了 糟心的开始写,老板说先不上太复杂,就先显示了两个页面,然后开开心心的发布,测试了几遍,没啥问题呀.结果,一上上去,就被老板批了! 啥呀! 这分享怎么这个鬼样子!明明我看文档都是 ...

  4. hdu 2774 后缀数组

    题目大意: 求两个字符串的最长公共子串 基本思路: 首先自然而然的想到应当把两个串拼接起来,为了便于处理,所以在串的中间加个大量(就是一个比其他值大的值),最后别忘了拼接上下标为n的时候是0,调用da ...

  5. winform中动态生成多行label,同时添加滚动条

    设计思路大概是这样的,Form内添加一个groupBox,groupBox内添加一个panel,panel的属性AutoScroll=true,在panel内动态添加label. 原始From如下: ...

  6. ajax 实战使用

    注意ajax 必须放在script脚本中使用 ajax用于前端朝后端提交数据,并且后端函数处理好结果返回给success函数作为回调函数给前端,前端拿到后端传来的值,比如code==0 来做相应的前端 ...

  7. 【Flutter学习】基本组件之基本按钮组件

    一,概述 由于Flutter是跨平台的,所以有适用于Android和iOS的两种风格的组件.一套是Google极力推崇的Material,一套是iOS的Cupertino风格的组件.无论哪种风格,都是 ...

  8. 线段树双tag+差分数组——cf1208E

    写了一上午 /* 对于每个数组a[],先排序然后从大到小把a[i]放进线段树更新 设a[i]的位置是pos,那么其可更新的区间是[pos,w-(li-pos)] 线段树结点保存 tag=now表示当前 ...

  9. NX二次开发-C语言文件读写fwrite和fread函数

    NX9+VS2012 #include <uf.h> #include <stdio.h> UF_initialize(); /* //设置文件路径 const char* f ...

  10. 为了避免hexo博客换了电脑应该提前做的准备。

    个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io 本文转自:https://www.jiansh ...