【二分查找】POJ2456-Aggressive cows
【题目大意】
有N间牛舍和M头牛,告诉你每个牛舍的位置,求出两头牛之间最小距离的最大值。
【思路】
二分判断两头牛之间的最小距离d,通过贪心法进行验证。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=+;
int n,m;
int pos[MAXN]; int whether(int d)
{
int now=;
int nowpos=pos[]+d;
for (int i=;i<m;i++)
{
while (pos[now]<nowpos && now<n) now++;
if (now==n)
{
return ;
}
nowpos=pos[now]+d;
}
return ;
} int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<n;i++) scanf("%d",&pos[i]);
sort(pos,pos+n); int lb=,ub=pos[n-];
while (ub-lb>)
{
int mid=(lb+ub)/;
if (whether(mid)) lb=mid;
else ub=mid;
}
cout<<lb<<endl;
return ;
}
【二分查找】POJ2456-Aggressive cows的更多相关文章
- 二分法的应用:最大化最小值 POJ2456 Aggressive cows
/* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- POJ2456 Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- 二分算法的应用——最大化最小值 POJ2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...
- POJ2456 Aggressive cows
Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...
- POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13993 Accepted: 6775 ...
- poj2456 Aggressive cows(二分查找)
https://vjudge.net/problem/POJ-2456 二分,从最大长度开始,不断折半试,如果牛全放下了,就是可行,修改下界,否则改上届. #include<iostream&g ...
- POJ2456 Aggressive cows(二分+贪心)
如果C(d)为满足全部牛之间的距离都不小于d. 先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d. #include<iostream> #include<cstdio> ...
- POJ2456 Aggressive cows(二分)
链接:http://poj.org/problem?id=2456 题意:一个数轴上n个点,每个点一个整数值,有c个奶牛,要放在这些点的某几个上,求怎么放可以使任意两个奶牛间距离的最小值最大,求这个最 ...
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
随机推荐
- quartz的简介
1. 介绍 Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源的任务调度框架,是完全由java开发的一个开源的任务日程管理系统,“任务进度管理器”就是一个在预 ...
- MySQL:Can't create test file XXX.lowe-test
问题说明 今天部署MySQL,在使用mysql_install_db,初始化数据库时报如下错误 180622 11:36:38 mysqld_safe Starting mysqld daemon w ...
- MYSQL表中设置字段类型为TIMESTAMP时的注意事项
在MYSQL中,TIMESTAMP类型是用来表示日期的,但是和DATETIME不同,不同点就不再这里说明了. 当我们在使用TIMESTAMP类型设置表中的字段时,我们应该要注意一点,首先我们在表中新增 ...
- jdbc预编译插入数据操作
package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepare ...
- Linux下批量Kill多个进程的方法
转自http://www.jb51.net/LINUXjishu/43534.html ps -ef|grep tt.py|grep -v grep|cut -c 9-15|xargs kill -9 ...
- Java读写锁(ReentrantReadWriteLock)学习
什么是读写锁 平时,我们常见的synchronized和Reentrantlock基本上都是排他锁,这些锁在同一时刻只允许一个线程进行访问,哪怕是读操作.而读写锁是维护了一对锁(一个读锁和一个写锁), ...
- Gray Code——陈瑶师姐面试时候要用回溯算法
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- electron 编译 sqlite3避坑指南---尾部链接有已经编译成功的sqlite3
electron 编译 sqlite3避坑指南(尾部链接有已经编译成功的sqlite3) sqlite很好用,不需要安装,使用electron开发桌面程序,sqlite自然是存储数据的不二之选,奈何编 ...
- crontab定时任务管理
定时任务 添加与编辑任务crontab -e0 */1 * * * command0 */2 * * * command 查询任务是否加了:crontab -l #查看当前用户crontab -l ...
- JSTL 1.1与JSTL 1.2之间的区别?如何下载JSTL 1.2?
JSTL 1.1与JSTL 1.2之间的区别?如何下载JSTL 1.2? JSTL 1.2中不要求standard.jar架包 您可以在Maven中央仓库中找到它们: http://repo2.mav ...