Aggressive cows

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 18313 Accepted: 8716

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.


解题心得:

  1. 有n间小屋,n个小屋排在一列,每个小屋距离原点有一个距离,要将m头牛放在小屋中,要使两头牛之间的距离尽量的大,如果最大化牛直接的距离,那么最近两头牛之间的距离最大是多少。
  2. 看到这个最大化最小值那么首先想到的就是二分,最大之间求最小,最小之间求最大,平均之间求最大都是二分。每次二分最大距离,然后按照这个距离安排,看能否用n个小屋安排下,不能那么减少距离,如果可以那么增大距离。

#include <algorithm>
#include <cstring>
#include <stdio.h>
using namespace std;
const int maxn = 1e5+10;
int pos[maxn],n,c; void init() {
for(int i=0;i<n;i++)
scanf("%d",&pos[i]);
sort(pos,pos+n);
} bool checke(int len){
int cnt = 1,now = pos[0];
for(int i=1;i<n;i++) {
if (pos[i] - now >= len) {
cnt++;
now = pos[i];
}
}
return cnt >= c;
} int binary_search() {
int l = 0,r = 1e9+10;
while(r-l > 1) {
int mid = (l+r)/2;
if(checke(mid))
l = mid;
else
r = mid;
}
return l;
} int main() {
while(scanf("%d%d",&n,&c) != EOF) {
init();
int ans = binary_search();
printf("%d\n",ans);
}
return 0;
}

POJ:2456-Aggressive cows的更多相关文章

  1. POJ:2456 Aggressive cows(z最大化最小值)

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

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

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

  3. 二分搜索 POJ 2456 Aggressive cows

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

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

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

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

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 D ...

  6. POJ 2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11192   Accepted: 5492 ...

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

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

  8. poj 2456 Aggressive cows 贪心+二分

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25944   Accepted: 11982 ...

  9. POJ 2456 Aggressive cows(贪心 + 二分)

    原题链接:Aggressive cows 题目大意:农夫 建造了一座很长的畜栏,它包括  个隔间,这些小隔间依次编号为. 但是, 的  头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...

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

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

随机推荐

  1. Python中的基本数据类型之列表与元组初步了解

    一.什么是列表 1.列表是Python中的基本数据类型之一用[]来表示,每一项元素由逗号隔开,列表什么都能装,(能装对象的对象) 2.可以用来存放大量数据 3.是一个可以改变的数据类型 二.列表的索引 ...

  2. If you want the rainbow, you have to deal with the rain.

    If you want the rainbow, you have to deal with the rain.想要彩虹,就先忍受雨水.

  3. Android OS Startup

    OS puts emphases on how to provide interfaces to user's APPs for using hardware device in the conven ...

  4. pcp分布式监控工具

    已经集成在redhat6.x版本里 http://pcp.io

  5. Ionic+AngularJS 开发的页面在微信公众号下显示不出来原因查究

    ionic 页面 微信浏览器遇到的坑 公司的微信公众号一部分页面是用AngularJS+Ioinc开发,发现在本地浏览器测试的时候都没问题,传到服务器在微信公众号下跑就出问题来,经查是: index- ...

  6. 验证tensorflow版本是GPU还是CPU

    reference: https://blog.csdn.net/zlase/article/details/79261348 import numpy import tensorflow as tf ...

  7. MySQL入门很简单: 10 mysql运算符

    1. 算术运算符 例子: 将t1表中字段a的值进行加法,减法和乘法 2. 比较运算符 注:LIKE经常和通配符"_"和"%"一起使用,"_" ...

  8. 测试笔记:本地存储localstorage与sessionstorage

    最近测试的投票项目开发说用的是localstorage.查了下是h5的本地存储.还有个sessionstorage,区别在于sessonstorage关闭页面后清空,localstorage保留. 以 ...

  9. POJ-1422 Air Raid---二分图匹配&最小路径覆盖

    题目链接: https://vjudge.net/problem/POJ-1422 题目大意: 有n个点和m条有向边,现在要在点上放一些伞兵,然后伞兵沿着图走,直到不能走为止 每条边只能是一个伞兵走过 ...

  10. 模拟水题,查看二维数组是否有一列都为1(POJ2864)

    题目链接:http://poj.org/problem?id=2864 题意:参照题目 哈哈哈,这个题discuss有翻译哦.水到我不想交了. #include <cstdio> #inc ...