[ACM] poj 2456 Aggressive cows (二分查找)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 5436 | Accepted: 2720 |
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
解题思路:
这道题虽然不长,可是读了好几遍才读懂。有n个牛舍,每个牛舍有自己的位置xi(可以理解为坐标),把c头牛放在这n个牛舍里面,要求最近的两头牛之间的最大距离。
比如测试数据 5个牛舍 3头牛 牛舍坐标1 2 8 4 9
先按贪心,第一头牛肯定被放在第一个牛舍里,排序 1 2 4 8 9
第二头牛可以放在2,也可以放在4,假设第二头牛放在2 ,与第一头牛的距离为1 ,假设第三头放在9,与第二头的距离为7,那么,最近的两头牛之间的最大距离为1
最有解为 第一头牛放在1 第二头牛放在4 ,第三头牛放在9 ,那么 4-1=3 9-4=5 ,最近的两头牛之间的最大距离为3
也就是求相邻两头牛之间距离的最小值(取最大)。这个距离采用二分的形式进行判断。
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn=100002;
const int inf=0x7fffffff;
int x[maxn];
int n,c; bool ok(int m)
{
int last=0;
for(int i=1;i<c;i++)
{
int cur=last+1;//cur指当前的牛舍位置
while(cur<n&&x[cur]-x[last]<m)
cur++;
if(cur==n)//只放下第一头牛,无法放下第二头牛,使二者的距离大于等于m
return false;
last =cur;//把第i头牛放在编号为cur的牛舍里,这里牛的头数和牛舍的编号都是从0开始的
}
return true;
}
int main()
{
cin>>n>>c;
for(int i=0;i<n;i++)
scanf("%d",&x[i]);
sort(x,x+n);
int l=0,r=inf;//这里r也可以写成题目里的最大距离1000000000
for(int i=0;i<100;i++)
{
int m=(l+r)/2;
if(ok(m))
l=m;
else r=m;
}
cout<<l<<endl;
return 0;
}
[ACM] poj 2456 Aggressive cows (二分查找)的更多相关文章
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- POJ 2456 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- POJ - 2456 Aggressive cows 二分 最大化最小值
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18099 Accepted: 8619 ...
- POJ 2456 Aggressive cows(二分答案)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...
- poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...
- [poj 2456] Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- POJ 2456 Aggressive cows ( 二分 && 贪心 )
题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
- 二分搜索 POJ 2456 Aggressive cows
题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...
随机推荐
- sql group by
group by实例 实例一 数据表: 姓名 科目 分数 张三 语文 80 张三 数学 98 张三 英语 65 李四 语文 70 李四 数学 80 李四 英语 90 期望查询结果: 姓名 语文 数学 ...
- SQL笔记 [SQL判断是否存在] [长期更新] (-2015.4)
--判断某个存储过程是否存在if exists (select * from sysobjects where id = object_id(N'[p_CreateTable]') and OBJEC ...
- JAVA编程规则【转自java编程思想】
本附录包含了大量有用的建议,帮助大家进行低级程序设计,并提供了代码编写的一般性指导: (1) 类名首字母应该大写.字段.方法以及对象(句柄)的首字母应小写.对于所有标识符,其中包含的所有单词都应紧靠在 ...
- js获取浏览器窗口可视区域大小
获得浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)的方法: 一.对于IE9+.Chrome.Firefox.Opera 以及 Safari: • window.innerHeight - 浏 ...
- ftp服务器远程拷贝命令
xiamense@xiamense-testserver:~$ ftp 218.5.82.40 输入账户密码 get 远程文件路径 本机服务器路径get pa20160927.xml /home/xi ...
- cocos2d-js 3.0rc0加载游戏引擎时长时间黑屏
如果是原始引擎的话是会比较大一些,但是最终发布的时候我们都建议你打包成release版,这个可以使用cocos命令 cocos compile -p web 来完成轻松打包,会在你的项目目录下创建一个 ...
- 一个用php实现的获取URL信息的类
获取URL信息的类 使用这个类,你能获得URL的如下信息: - Host - Path - Statuscode (eg. 404,200, ...) - HTTP Version - Ser ...
- UBUNTU9.10下安装TFTP学习笔记一(arm学习SEED-138板子)
擦,刚刚写的没保存都丢了,郁闷中~~~~ 简单重写 1什么是TFTP .安装TFTP(TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的 ...
- jquery mobile开发笔记之Ajax提交数据(转)
http://my.oschina.net/xiahuawuyu/blog/81763 这两天学习了下,jquery mobile(以下简称jqm)的开发相关的内容.可能之前有过web的开发基础,相对 ...
- [2015hdu多校联赛补题]hdu5299 Circles Game
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5299 题意: 在欧几里得平面上有n个圆,圆之间不会相交也不会相切,现在Alice和Bob玩游戏,两人轮 ...