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.
 
 

题意:

  又见农场主John..  他有C头牛,N个牛栏排成一条直线,每个的位置为Xi,他的牛比较好斗,把C头牛放到N个牛栏里两两之间要离得尽可能的远,问求这个最大的最小距离。

思路:

  求最大最小距离,可以从大到小枚举可能的最小距离,如果能把C头牛都放下,则这个距离就是最大最小距离,否则继续减小枚举距离。但是这样时间复杂的也太大了,考虑采用二分来找距离,L = 0, R = 1000000010, mid = (L+R)/2, 如果mid这个距离可以的话,可能有更大的距离满足,因此在[mid, R]中寻找,如果不可以需要缩小范围,在[L, mid)中寻找,如此进行下去,最终到R-L=1是停止,在之前mid是可以的,L = mid 最后输出L即可。

  对于搜寻距离d是否可行时,由贪心1是肯定要选的,然后在找C-1头牛的位置,last为前一个的指针,cur为当前指针,找到cur的第一个位置,如果超出范围了就不可以, 全部找完了此种方案可行。

代码:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
int a[];
int N, C; bool judge(int d)
{
int last = , cur;
for (int i = ; i < C; i++) {
cur = last + ;
while (cur < N && a[cur]-a[last]<d)
cur++;
if (cur == N) return ;
last = cur;
}
return ;
} int main()
{
//freopen("1.txt", "r", stdin);
scanf("%d%d", &N, &C);
for (int i = ; i < N; i++) {
scanf("%d", &a[i]);
}
sort(a, a+N);
int L = , R = ;
while (R - L > ) {
int mid = (L+R)/;
if (judge(mid))
L = mid;
else
R = mid;
}
printf("%d\n", L); return ;
}

   

[poj 2456] Aggressive cows 二分的更多相关文章

  1. POJ 2456 Aggressive cows (二分 基础)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7924   Accepted: 3959 D ...

  2. POJ 2456 Aggressive cows(二分答案)

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...

  3. POJ - 2456 Aggressive cows 二分 最大化最小值

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18099   Accepted: 8619 ...

  4. poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...

  5. [POJ] 2456 Aggressive cows (二分查找)

    题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...

  6. POJ 2456 Aggressive cows ( 二分 && 贪心 )

    题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...

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

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

  8. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

  9. POJ 2456 Agressive cows(二分)

    POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...

随机推荐

  1. laravel count的使用

    rt 学习源头: https://blog.csdn.net/chajinglong/article/details/51954010 四.聚合 查询构建器还提供了各种聚合方法,如统计,马克斯,min ...

  2. Linux系统下如何去掉文件的@属性

    前几天从别处copy了一个memcached.so文件,发现运行不了,用ls -l 看了一下发现memcached.so多了一个@属性,如何去掉这个@属性呢? 第一步:ls -laeO@ 第二步:xa ...

  3. hihoCoder#1095(二分搜索)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playing a drinking game called HI ...

  4. hihoCoder1296:约瑟夫问题

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho的班级正在进行班长的选举,他们决定通过一种特殊的方式来选择班长. 首先N个候选人围成一个圈,依次编号为0.. ...

  5. 遍历js的obj中所有属性得key

    var obj = $("#jstree_default").jstree("get_checked"); for (var a in obj) { alert ...

  6. DCloud-wap2app:杂项

    ylbtech-DCloud-wap2app:杂项 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   ...

  7. js生成邀请码(2)

    //生成邀请码方法一 /*function createInviteCode() { var s = [],a=6,b=10; var chars = "123456789QWERTYUIP ...

  8. 2015.3.11 VS异步控件及进度条结合应用

    1.在Form中添加 指针控件:BackgroundWorker-bgwork:进度条控件progressBar1 以及开始.取消按钮 2.开始按钮启动异步线程 private void button ...

  9. SqlServer——游标

    参考:http://www.cnblogs.com/94cool/archive/2010/04/20/1715951.html  http://www.cnblogs.com/moss_tan_ju ...

  10. 使用pip一次升级所有安装的Python包(太牛了)

    import pip from subprocess import call for dist in pip.get_installed_distributions(): call("pip ...