Aggressive cows

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20485   Accepted: 9719

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.

 题意

有n个牛棚,每个牛棚的位置为a[i],m头牛,要求把这m头牛放进牛棚中,并要求这些牛之间互相间隔的距离最大,求这个最大距离

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
int a[maxn];
int n,m;
bool C(int d)
{
int last=0;
for(int i=1;i<m;i++)
{
int crt=last+1;
while(crt<n&&a[crt]-a[last]<d)
crt++;
if(crt==n)
return false;
last=crt;
}
return true;
}
void solve()
{
sort(a,a+n);
int lb=0;
int ub=INF;
while(ub-lb>1)
{
int mid=(lb+ub)/2;
if(C(mid))
lb=mid;
else
ub=mid;
}
cout<<lb<<endl;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>a[i];
solve();
return 0;
}

POJ 2456: Aggressive cows(二分,贪心)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. [poj 2456] Aggressive cows 二分

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

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

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

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

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

  10. 二分搜索 POJ 2456 Aggressive cows

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

随机推荐

  1. spring boot整合shiro后,部分注解(Cache缓存、Transaction事务等)失效的问题

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/elonpage/article/details/78965176 前言 整合有缓存.事务的sprin ...

  2. php composer 使用 以及 psr0和psr4的真正区别

    composer 使用 项目和库之间唯一的区别是,你的项目是一个没有名字的包 包名不区分大小写,但惯例是使用小写字母,并用连字符作为单词的分隔 入门比较好资源: 官网适合读一遍  https://do ...

  3. 蓝桥杯—BASIC-25 回形取数

    题目:回形取数就是沿矩阵的边取数,若当前方向上无数可取或已经取过,则左转90度.一开始位于矩阵左上角,方向向下.输入格式 输入第一行是两个不超过200的正整数m, n,表示矩阵的行和列.接下来m行每行 ...

  4. error TS2304: Cannot find name 'Promise' && TS2307: Cannot find module '**'

    error TS2304: Cannot find name 'Promise' 解决方法:在编译选项中加入"target":"es6" { "ver ...

  5. day26 第二阶段共享

    第二阶段共享--网络编程 一.C/S架构 : 客户端(client)/服务端(sever)框架 B/S架构: 浏览器(brower)/服务端(sever)架构 软件CS架构: 服务端连接到浏览器,QQ ...

  6. Azulão--青鸟--IPA--巴西葡萄牙语

    这是巴西很有名的民谣.

  7. ubuntu14.04下搜狗输入法不能输入中文问题解决

    解决方法如下:   一.重启搜狗输入法 通过下面的两个命令重启搜狗输入法 ~$ killall fcitx  ~$ killall sogou-qinpanel   二.检查修复安装依赖 ~$ sud ...

  8. 玩转X-CTR100 l STM32F4 l MPU6050加速度陀螺仪传感器

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      本文介绍X-CTR100控制器 板载加速度 ...

  9. WIN10-缩放与布局

    HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics\AppliedDPI230%----- 221225%----- 218220%----- ...

  10. Python写一个批量生成账号的函数

    批量生成账户信息,产生的账户由@sina.com结尾,长度由用户输入,产生多少条也由用户输入,用户名不能重复,用户名必须由大写字母.小写字母和数字组成. def Users(num,len): # n ...