POJ_2456_Agressive_cows_(二分,最大化最小值)
描述
http://poj.org/problem?id=2456
有n个小屋,线性排列在不同位置,m头牛,每头牛占据一个小屋,求最近的两头牛之间距离的最大值.
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10095 | Accepted: 4997 |
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.
Source
分析
二分.
最大化最小值.
C(x)表示使两头牛之间最小距离为x,问题转化为求满足C(x)的x的最大值.可知x<=(总长)/(c-1).那么1~(总长)/(c-1)二分求解即可.
#include<cstdio>
#include<algorithm>
using std :: sort; const int maxn=;
int n,c;
int a[maxn]; void init()
{
scanf("%d%d",&n,&c);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a+n+);
} bool judge(int x)
{
int last=;
for(int i=;i<=c;i++)
{
int now=last+;
while(now<=n&&(a[now]-a[last]<x)) now++;
if(now>n) return false;
last=now;
}
return true;
} int bsearch(int x,int y)
{
while(x<y)
{
int m=x+(y-x+)/;
if(judge(m)) x=m;
else y=m-;
}
return x;
} void solve()
{
int INF=(a[n]-a[])/(c-);
printf("%d\n",bsearch(,INF));
} int main()
{
freopen("cow.in","r",stdin);
freopen("cow.out","w",stdout);
init();
solve();
fclose(stdin);
fclose(stdout);
return ;
}
POJ_2456_Agressive_cows_(二分,最大化最小值)的更多相关文章
- River Hopscotch(二分最大化最小值)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9923 Accepted: 4252 D ...
- POJ3258 River Hopscotch(二分最大化最小值)
题目链接:http://poj.org/problem?id=3258 题意:给n个石头,起点和终点也是两个石头,去掉这石头中的m个,使得石头间距的最小值最大. 思路:二分石头间的最短距离,每次贪心地 ...
- POJ_3258_River_Hopscotch_[NOIP2015]_(二分,最大化最小值)
描述 http://poj.org/problem?id=3258 给出起点和终点之间的距离L,中间有n个石子,给出第i个石子与起点之间的距离d[i],现在要去掉m个石子(不包括起终点),求距离最近的 ...
- CodeForces 689C Mike and Chocolate Thieves (二分最大化最小值)
题目并不难,就是比赛的时候没敢去二分,也算是一个告诫,应该敢于思考…… #include<stdio.h> #include<iostream> using namespace ...
- poj3104 Drying(二分最大化最小值 好题)
https://vjudge.net/problem/POJ-3104 一开始思路不对,一直在想怎么贪心,或者套优先队列.. 其实是用二分法.感觉二分法求最值很常用啊,稍微有点思路的二分就是先推出公式 ...
- POJ - 2456 Aggressive cows 二分 最大化最小值
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18099 Accepted: 8619 ...
- poj3045 Cow Acrobats(二分最大化最小值)
https://vjudge.net/problem/POJ-3045 读题后提取到一点:例如对最底层的牛来说,它的崩溃风险=所有牛的重量-(底层牛的w+s),则w+s越大,越在底层. 注意范围lb= ...
- POJ3285 River Hopscotch(最大化最小值之二分查找)
POJ3285 River Hopscotch 此题是大白P142页(即POJ2456)的一个变形题,典型的最大化最小值问题. C(x)表示要求的最小距离为X时,此时需要删除的石子.二分枚举X,直到找 ...
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
随机推荐
- Struts1运行原理以及整合步骤
Struts1 struts1运行步骤 1.项目初始化:项目启动时加载web.xml,struts1的总控制器ActionServlet是一个Servlet,它在web.xml中是配置成自动启动的S ...
- c++ primer (5)2
第三章 1.头文件不应包含using声明,因为头文件的内容会拷贝到所有引用它的文件中去. 2.初始化string对象的方式: string s1; //默认初始化,s1是一个空串 string s2( ...
- [Python][flask][flask-wtf]关于flask-wtf中API使用实例教程
简介:简单的集成flask,WTForms,包括跨站请求伪造(CSRF),文件上传和验证码. 一.安装(Install) 此文仍然是Windows操作系统下的教程,但是和linux操作系统下的运行环境 ...
- Git常用命令汇总
1.初始化相关 git init 初始化仓库 git remove add origin url 添加仓库地址 git remove rm origin 删除仓库地址 git clone 克隆别人的分 ...
- stream的Read、Write方法实例
, bytes.Length)) > ) , readBytes-);//8为偏移量,10为数量 } } ...
- Java学习--final与static
final是java的关键字,它所表示的是“这部分是无法修改的”. 编译期常量,它在类加载的过程就已经完成了初始化,所以当类加载完成后是不可更改的,编译期可以将它代入到任何用到它的计算式中,也就是说可 ...
- Html5 布局经验分享-第1集
移动端的布局与pc端的布局相比 移动端的布局就简单的多,兼容性不必考虑那么多,css3各种特性基本上是可以放肆的写:(个人看法) 本人做移动web 布局一段时间了,把这其中遇到的一些问题总结下来,均是 ...
- MVC-Model数据注解(一)-系统(DataAnnotations)
要使用验证,首先,web.config要开户验证: <appSettings> <add key="ClientValidationEnabled" value= ...
- Xcode常见错误以及解决方案
一.Undefined symbols for architecture x86_64: Xcode升级到5.1 新特性之一就是默认让所有App都通过64位编译器编译.原来在Xcode5.0.x的时候 ...
- C#中如何按字节数截取字符串?
http://www.cnblogs.com/xuejie/archive/2012/12/14/2818452.html