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

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



题目链接:

id=2456">http://poj.org/problem?

id=2456



题目大意:一个数轴上n个点,每一个点一个整数值,有c个物品。要放在这些点的某几个上,求怎么放能够使随意两个物品间距离的最小值最大,求这个最大值



题目分析:最小值最大,典型二分题。二分距离的值推断

#include <cstdio>
#include <algorithm>
using namespace std;
int const INF = 0x3fffffff;
int const MAX = 1e5 + 5;
int d[MAX];
int n, c; int cal(int m)
{
int ans = 0, now = d[0];
for(int i = 1; i < n; )
{
while(d[i] < now + m)
i ++;
ans ++;
now = d[i];
}
return ans;
} int main()
{
scanf("%d %d", &n, &c);
for(int i = 0; i < n; i++)
scanf("%d", &d[i]);
sort(d, d + n);
int r = d[n - 1] - d[0], l = 0, ans = 0;
while(l <= r)
{
int m = (l + r) / 2;
int num = cal(m);
if(num >= c)
{
ans = m;
l = m + 1;
}
else
r = m - 1;
}
printf("%d\n", ans);
}

POJ 2456 Aggressive cows (二分 基础)的更多相关文章

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

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

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

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

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

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

  4. [poj 2456] Aggressive cows 二分

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...

  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. gradle ofbiz 16 开发环境搭建

    原 gradle ofbiz 16 开发环境搭建 2017年02月13日 10:59:19 阅读数:2702 1.安装jdk 2.配置jdk环境变量 3.eclipse 安装svn 插件 4.svn下 ...

  2. sql2008百万级数据排除重复信息

    --高性能排除重复select userid from table where userid in ( select userid from ( select userid, row_number() ...

  3. input输入框与元素间有间隙

    <div class="container"> <button>1</button> <button>2</button> ...

  4. 在razor中使用递归,巧用递归

    原文发布时间为:2011-04-20 -- 来源于本人的百度文章 [由搬家工具导入] Learning Razor–Writing an Inline Recursive HTML Helper Wr ...

  5. js禁用"Backspace"键(即禁止网页倒退)

    项目遇到的一个问题一个普通网页,如果这个网页上没有焦点的话,那么点击"Backspace"键的时候,网页会回退(倒退到上一个网页),这样就会就有一个问题,当我在一个输入框进行输入的 ...

  6. uva 1611:Crane(构造 Grade D)

    题目链接 题意: 一个序列,你可以选择其中偶数长度的一段,然后中间切开,左右两段交换.现给你一个1~n的某个排列,求一个交换方案,使得排列最终有序.(交换次数 < 9^6) 思路: 从左到右,依 ...

  7. 在DB2中使用EXPORT实现将数据导出文本文件

    EXPORT TO "D:/test.txt" OF DEL SELECT loginname,password FROM cm_staff where loginname = ' ...

  8. 洛谷 P1031 均分纸牌【交叉模拟】

    题目描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若干张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸牌,只能移到编号为 2 ...

  9. HDFS读文件过程分析:读取文件的Block数据

    转自http://shiyanjun.cn/archives/962.html 我们可以从java.io.InputStream类中看到,抽象出一个read方法,用来读取已经打开的InputStrea ...

  10. codevs——1169 传纸条(棋盘DP)

    2008年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 小渊和小 ...