POJ 2456 Aggressive cows---二分搜索法
///3.最大化最小值
/**
POJ 2456 Aggressive cows
Q:一排牛舍有N (2 <= N <= 100,000) 个,位置为x1,...,xN (0 <= xi <= 1,000,000,000)
为使牛之间不受到伤害,需最大化他们之间的距离,求最大化最近两头牛之间的距离
共M只牛
A:
条件C(x):可以使得最近两头牛之间的距离不小于d->求满足条件的最大d->如何高效的判断C(x) */
#include"iostream"
#include "cstdio"
#include "algorithm"
using namespace std;
#define MAX 100010
#define INF 0x3f3f3f3f
int N,M,x[MAX];
bool C(int d)
{
int last=;///首位定放牛;看能否找到M-1个牛舍 能够满足之间距离>=d?
for(int i=;i<M;i++)
{
int crt=last+;///cnt表示当前牛舍位置下标
while(crt<N&&x[crt]-x[last]<d)///last表示上一被占牛位舍位置下标,
{
crt++;
}
if(crt==N)return false;///牛舍不能满足
last=crt;
}
return true;
}
void solve()
{
int lb=,ub=INF;
int mid;
while(ub-lb>)
{
mid=(lb+ub)/;
if(C(mid))///放得下,距离还可以再大点
lb=mid;
else ///放不下了,mid已经不能满足
ub=mid;
}
printf("%d\n",lb);
} int main()
{
while(~scanf("%d%d",&N,&M))
{
for(int i=;i<N;i++)
{
scanf("%d",&x[i]);
}
sort(x,x+N);
solve();
}
}
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 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- [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 ...
随机推荐
- 仿造vue-resource的formdata传对象
众插件不支持同步,也是没办法的事情,具体为啥就不分析了,确实搞不懂. 一直用vue-resource的post,觉得很舒服. 然,没办法只能仿造一个,自己提供一个同步方法 几个点先摆清楚 1. .th ...
- Git中从远程的分支获取最新的版本到本地——两种命令
Git中从远程的分支获取最新的版本到本地有这样2个命令: 1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge Git fetch origin master git log ...
- C++11中initializer lists的使用
Before C++11,there was no easy way to do things like initialize a std::vector or std::map(or a custo ...
- pprof 查看goroutine
package main import ( "net/http" "runtime/pprof" ) var quit chan struct{} = make ...
- EF更新时出错,An error occurred while updating the entries. See the inner exception for details
在使用EF进行更新数据时出错,报出的异常是 "An error occurred while updating the entries. See the inner excep ...
- 【数据库】 SQLite 语法
[数据库] SQLite 语法 一 . 创建数据库 1. 只需创建数据库,只需创建文件,操作时将连接字符串指向该文件即可 2. 连接字符串 : data source = FilePath; 不能加密 ...
- [转]使用Gradle管理你的Android Studio工程
本文转自:http://www.flysnow.org/2015/03/30/manage-your-android-project-with-gradle.html Gradle简介 Gradle ...
- iframe底边多出4px或5px解决办法
问题: 在处理iframe框架自适应时,并且已经去掉iframe的边框,但仍然出现底边多出4px或5px高度的情况.如图 <div id="content"> < ...
- Hyper-v创建检查点(VM的快照功能)
一. 问题描述 在Hyper的默认设置中没有创建检查点的功能,是因为他启用的并不是标准设置.设置检查点时会 二. 解决方法 再设置检查点就没问题了
- HDU 4441 Queue Sequence(优先队列+Treap树)(2012 Asia Tianjin Regional Contest)
Problem Description There's a queue obeying the first in first out rule. Each time you can either pu ...