最大化最小值poj2456Aggressive cows
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 15528 | Accepted: 7440 |
Description
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
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
Sample Input
5 3
1
2
8
4
9
Sample Output
3
Hint
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.
check(d)=可以安排所有的牛的位置使得最近的两头牛的距离都不小于d
求满足check(d)的最大的d的值。
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
int house[maxn];
int n,m;
const int inf=99999999;
bool check(int d)
{
int last=0;
for(int i=1;i<m;i++)
{
int crt=last+1;
while(crt<n&&house[crt]-house[last]<d)
{
crt++;
}
if(crt==n)return false;
last=crt;
}
return true;
}
void solve()
{
sort(house,house+n);
int lb=0,ub=inf;
while(ub-lb>1)
{
int mid=(ub+lb)/2;
if(check(mid))
lb=mid;
else
ub=mid;
}
printf("%d\n",lb);
}
int main()
{
cin>>n>>m;
for(int j=0;j<n;j++)
scanf("%d",&house[j]);
solve();
return 0;
}
最大化最小值poj2456Aggressive cows的更多相关文章
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
- 二分法的应用:最大化最小值 POJ2456 Aggressive cows
/* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- POJ_2456_Agressive_cows_(二分,最大化最小值)
描述 http://poj.org/problem?id=2456 有n个小屋,线性排列在不同位置,m头牛,每头牛占据一个小屋,求最近的两头牛之间距离的最大值. Aggressive cows Tim ...
- POJ2456(最大化最小值)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10728 Accepted: 5288 ...
- POJ3285 River Hopscotch(最大化最小值之二分查找)
POJ3285 River Hopscotch 此题是大白P142页(即POJ2456)的一个变形题,典型的最大化最小值问题. C(x)表示要求的最小距离为X时,此时需要删除的石子.二分枚举X,直到找 ...
- hihocoder 二分·二分答案【二分搜索,最大化最小值】 (bfs)
题目 这道题做了几个小时了都没有做出来,首先是题意搞了半天都没有弄懂,难道真的是因为我不打游戏所以连题都读不懂了? 反正今天是弄不懂了,过几天再来看看... 题意:一个人从1点出发到T点去打boss, ...
- codeforce 1070 E Getting Deals Done(二分求最大化最小值)
Polycarp has a lot of work to do. Recently he has learned a new time management rule: "if a tas ...
- nyoj-586-疯牛|poj-2456-Aggressive cows
http://acm.nyist.net/JudgeOnline/problem.php?pid=586 http://poj.org/problem?id=2456 解题思路:最大化最小值二分答案即 ...
- 二分算法的应用——最大化最小值 POJ2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...
随机推荐
- CentOS7下安装单机版RabbitMQ及权限赋予
RabbitMQ官网rpm软件包地址:https://www.rabbitmq.com/releases/ 一.安装环境: CentOS7.erlang-19.0.4-1.el7.centos.x86 ...
- Ubuntu 16.04出现:qmake: could not exec '/usr/lib/x86_64-linux-gnu/qt4/bin/qmake': No such file or directory
没有安装qt4-qmake,安装即可: sudo apt-get install qt4-qmake 参考: https://stackoverflow.com/questions/23703864/ ...
- Servlet实现国际化
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/internationalization.html: 三个重要术语: 国际化(i18n):这意味着 ...
- golang convert integer to float number
There is no float type. Looks like you want float64. You could also use float32 if you only need a s ...
- 016 ppp authentication
Press RETURN to get started! Router>en Router#config t Enter configuration commands, one pe ...
- spring test---restful与文件上传
spring提供了大量经常使用的功能測试,如文件上传.restful风格url訪问.以下介绍主要介绍下test中经常使用功能的使用方法: 首先能够静态导入类.方便在測试类中使用,导入的类有 impor ...
- Skype for Business七大新功能
Lync Server 2013的下一版本号.Skype for Business将于2015年4月正式公布,下面是七大新功能. "呼叫监听"(Call Monitor)--假设你 ...
- 为什么有些信号线串接33R小电阻?
本人只是刚入坑的硬件助理,一名小白,很多都知识不懂,以下信息多数来自网络,可能不准确,恳请批评指正! 正文: 参考资料:http://blog.csdn.net/xiangyuqxq/article/ ...
- [LeetCode][Java] Jump Game II
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- shell 爬虫 从日志分析到数据采集与分析
[root@VM_61_158_centos ~]# curl http://ip.chinaz.com/220.112.233.179 |grep -e Whwtdhalf.*span.*span. ...