[poj 2456] Aggressive cows 二分
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.
题意:
又见农场主John.. 他有C头牛,N个牛栏排成一条直线,每个的位置为Xi,他的牛比较好斗,把C头牛放到N个牛栏里两两之间要离得尽可能的远,问求这个最大的最小距离。
思路:
求最大最小距离,可以从大到小枚举可能的最小距离,如果能把C头牛都放下,则这个距离就是最大最小距离,否则继续减小枚举距离。但是这样时间复杂的也太大了,考虑采用二分来找距离,L = 0, R = 1000000010, mid = (L+R)/2, 如果mid这个距离可以的话,可能有更大的距离满足,因此在[mid, R]中寻找,如果不可以需要缩小范围,在[L, mid)中寻找,如此进行下去,最终到R-L=1是停止,在之前mid是可以的,L = mid 最后输出L即可。
对于搜寻距离d是否可行时,由贪心1是肯定要选的,然后在找C-1头牛的位置,last为前一个的指针,cur为当前指针,找到cur的第一个位置,如果超出范围了就不可以, 全部找完了此种方案可行。
代码:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
int a[];
int N, C; bool judge(int d)
{
int last = , cur;
for (int i = ; i < C; i++) {
cur = last + ;
while (cur < N && a[cur]-a[last]<d)
cur++;
if (cur == N) return ;
last = cur;
}
return ;
} int main()
{
//freopen("1.txt", "r", stdin);
scanf("%d%d", &N, &C);
for (int i = ; i < N; i++) {
scanf("%d", &a[i]);
}
sort(a, a+N);
int L = , R = ;
while (R - L > ) {
int mid = (L+R)/;
if (judge(mid))
L = mid;
else
R = mid;
}
printf("%d\n", L); return ;
}
[poj 2456] Aggressive cows 二分的更多相关文章
- 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: 22674 Accepted: 10636 Des ...
- POJ - 2456 Aggressive cows 二分 最大化最小值
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18099 Accepted: 8619 ...
- poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- 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 ...
- POJ 2456 Agressive cows(二分)
POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...
随机推荐
- 使用HibernateTemplate手写源生SQL的【增删改查】 操作
使用 HibernateTemplate 进行持久化操作 执行的时候不报错,但数据库的持久化操作没有一点作用,问了好多人,说没有声明事务和提交事务, 用的是别人搭的的架构,事务已经有了,自动提交事务的 ...
- 【转】 Pro Android学习笔记(八四):了解Package(3):包间数据共享
目录(?)[-] 共享User ID的设置 共享资源例子 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowing ...
- HTTP-Runoob:HTTP状态码
ylbtech-HTTP-Runoob:HTTP状态码 1.返回顶部 1. HTTP状态码 当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求.当浏览器接收并显示网页前,此网页所在的服务 ...
- Mongodb 3.6 副本集测试及添加删除节点等操作
下载tar包并安装curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.8.tgz [root@mysqlt ...
- struts2中常用constant命令配置
struts.objectFactory这个属性用 于说明Struts2的 对象池创建工厂,Struts2也有自己的对象池,就像Spring那样,在配置文件中你可以引用对象池中的对象,你可以借助于Sp ...
- 数据库:ubantu下MySQL安装指南
http://wiki.ubuntu.org.cn/MySQL%E5%AE%89%E8%A3%85%E6%8C%87%E5%8D%97 安装MySQL sudo apt-get install mys ...
- java 多线程系列基础篇(十)之线程优先级和守护线程
1. 线程优先级的介绍 java 中的线程优先级的范围是1-10,默认的优先级是5.“高优先级线程”会优先于“低优先级线程”执行. java 中有两种线程:用户线程和守护线程.可以通过isDaemon ...
- UIBezierPath和CAShapeLayer配合肆意画图
一.CAShapeLayer CAShapeLayer 是 CALayer 的子类,但是比 CALayer 更灵活,可以画出各种图形 使用CAShapeLayer 绘制一个矩形 let layer ...
- #pragma execution_character_set("utf-8")
VC2010增加了“#pragma execution_character_set("utf-8")”,指示char的执行字符集是UTF-8编码. VS2010 设置 字符编码: ...
- 用JS,求斐波那契数列第n项的值
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...