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 ...
随机推荐
- html5特效库
Swiper是纯javascript打造的滑动特效插件,面向手机.平板电脑等移动终端.能实现触屏焦点图.触屏Tab切换.触屏多图切换等常用效果. delaunay.js是一款能在图片中形成无数个三角形 ...
- iOS-合成图片(长图)
合成图片 直接合成图片还是比较简单的,现在的难点是要把,通过文本输入的一些基本数据也合成到一张图片中,如果有多长图片就合成长图. 现在的实现方法是,把所有的文本消息格式化,然后绘制到一个UILable ...
- 论文翻译_Tracking The Untrackable_Learning To Track Multiple Cues with Long-Term Dependencies_IEEE2017
Tracking The Untrackable: Learning to Track Multiple Cues with Long-Term Dependencies 跟踪不可跟踪:学习跟踪具有长 ...
- Long Short-Term Memory (LSTM)
Long Short-Term Memory (LSTM) Outline Background LSTM Network Extended LSTM LST ...
- tar 加密压缩和解密解压
加密压缩 tar -czvf - file | openssl des3 -salt -k password -out /path/to/file.tar.gz 解密解压 openssl des3 - ...
- JMS实战——ActiveMQ
安装 官网下载地址:http://activemq.apache.org/ 小编这里以5.9.0版本为例,做简单介绍. 下载之后解压到制定路径,目录结构如下: 启动 直接运行bin下的activemq ...
- JVM(2)——GC算法和收集器
一.引入 上篇博客<JVM--简介>中主要介绍了JVM的内存模型,思考一下: 为什么要划分堆.栈.方法区等? 为什么把不同种类的数据信息分别存放? 答案可以分为很多很多条,这里就说一个方面 ...
- Jprofiler分析WebSphere(配置WebSphereagent代理)
一. Windows+WebSphere+Jprofiler9 我们自己的windows机器监控本地的WebSphere,应该为服务器配置监控代理,供Jprofiler连接使用,具体步骤如下: 1 ...
- BZOJ4345 POI2016Korale(构造+堆+线段树)
注意到k与n同阶,考虑构造一种枚举子集的方式,使得尽量先枚举较小的子集.首先sort一下,用堆维护待选子集.每次取出最小子集,并加入:1.将子集中最大数ai替换为ai+1 2.直接向子集中添加ai+1 ...
- RDMA
什么是RDMA? 来源 https://blog.csdn.net/u011459120/article/details/78469098 1. 概述 RDMA是Remote Direct Memor ...