二元矩阵峰值搜索

A farmer has built a long barn with N stalls. The stalls are placed in a straight manner at positions from x1, x2, ...xN. But his cows (C) are aggressive and don’t want to be near other cows. To prevent cows from hurting each other, he wants to place them in such a way so that the minimum distance between them is as large as possible. What is the largest minimum distance?

一位农民用N个摊位建造了一个长谷仓。 档位以直线方式放置在x1,x2,... xN处 。 但是他的母牛(C)具有攻击性,不想靠近其他母牛。 为了防止母牛互相伤害,他想以这样的方式放置它们,使它们之间的最小距离尽可能大。 最大最小距离是多少?

Constraints:

限制条件:

    N: 2<=N<=100,000
C: 2<=C<=N
Xi: 0<=Xi<=1,000,000,000

Input:

输入:

    Line1: Two integers N and C
Next N lines: An integer for Xi stall's position

Example:

例:

    Input:
5 3
1 2 4 8 9
Output:
3

Explanation:

说明:

If the cows are placed at 1,4 and 9, it will result in minimum distance from 3.

如果将母牛放在1,4和9处,则距离3的距离最小。

SOLUTION AND CODE

解决方案和代码

Since the range of xi is from 0 to 10^9, one can say that the minimum distance between the cows will lie between the range. The minimum distance between 2 stalls can be 0 (lower bound) and maximum distance is 10^9 (upper bound). So, one can check for each value from lower bound to upper bound.

由于xi的范围是0到10 ^ 9,因此可以说母牛之间的最小距离将介于该范围之间。 2个档位之间的最小距离可以为0(下限),最大距离为10 ^ 9(上限)。 因此,可以检查从下限到上限的每个值。

Let's say for k minimum distance, we can check if it is possible to place cows in the stall. In case, you have reached the last stall but didn’t have placed all cows, then it is not possible else it is possible.

假设有k个最小距离,我们可以检查是否可以将奶牛放在摊位中。 万一您到达了最后一个摊位,但没有放完所有母牛,那么就不可能了。

A point to note is that if, for a k, it is possible to place the cows. Then all the values less than k will be true. Also, if k is false, then all the values greater than k will be false. We can say it is creating a monotonic function and we have to check for the transition from true to false and return that value.

需要注意的一点是,如果以ak为单位,则可以放置母牛。 那么所有小于k的值都将为真。 同样,如果k为假,则所有大于k的值都为假。 我们可以说它正在创建一个单调函数,我们必须检查从true到false的转换并返回该值。

It can be quickly and easily done with Binary Search from the range of 0 to 10^9.

使用Binary Search可以从0到10 ^ 9的范围内快速轻松地完成此操作。

CODE

#include <bits/stdc++.h>
using namespace std;
int N,C;
int check(int num,int stalls[])
{int cows=1,pos=stalls[0];
for (int i=1; i<N; i++)
{if (stalls[i]-pos>=num)
{pos=stalls[i];
cows++;
if (cows==C)
return 1;
}
}
return 0;
}
int binarySearch(int stalls[])
{int start=0,end=stalls[N-1],max=-1;
while (end>start)
{int mid=(start+end)/2;
if (check(mid,stalls)==1)
{if (mid>max)
max=mid;
start=mid+1;
}
else
end=mid;
}
return max;
}
int main()
{int t;
cin>>t;
while (t--)
{cin>>N>>C;
int stalls[N];
for (int i=0; i<N; i++)
cin>>stalls[i];
sort(stalls,stalls+N);
int k=binarySearch(stalls);
cout<<k;
}
return 0;
}

Output

输出量

翻译自: https://www.includehelp.com/data-structure-tutorial/aggressive-cows-on-binary-search.aspx

二元矩阵峰值搜索

二元矩阵峰值搜索_好斗的牛(二元搜索)相关推荐

  1. 二元函数泰勒公式例题_考研数一对二元函数的二阶泰勒公式的要求是了解,那我们要了解到什么程度呢?会出那种类型的题呢?...

    展开全部 二阶泰勒来公式不需要很32313133353236313431303231363533e78988e69d8331333433626531深的了解,基本上是考不到的,从97到11年的真题来看 ...

  2. 华为手机关闭蓝牙开发搜索_大众速腾手机无法搜索车载蓝牙系统维修实例

    故障现象 全新速腾1.4T,行驶才600公里,客户来站反应手机无法搜索车载蓝牙系统. 故障诊断 1. 打开收音机启动蓝牙系统,有蓝牙图标指示,用手机搜索时无法搜到车载蓝牙系统.查看右前座椅下方有蓝牙电 ...

  3. python 对 文件内容 搜索_在txt文件中搜索数据

    如果您设计了数据格式,固定宽度的列不是一个很好的格式.但如果你被他们困住了,他们很容易对付.在 首先,要分析数据:addressbook = [] with open('addressbook.txt ...

  4. rss源搜索_如何使用Google图像搜索,RSS源和更多自定义墙纸

    rss源搜索 If you're looking for a free but powerful solution to automate your Windows wallpaper switchi ...

  5. java 多关联字联合搜索_【lucene】高级搜索篇

    一,概念 1. Lucene相关排序流程 找到关键词匹配的文档集合---->文档集合每个文档计算检索相似度----->对文档集合进行排序 2. Lucene相关类 ① Query类:一个抽 ...

  6. 广度优先搜索_计算机入门必备算法——广度优先遍历搜索

    1.  序言 又很久没有学习了,上次学到哈希表又称散列表的相关知识,这次我们学习一种新的数据结构来建立网络模型.这种数据结构被称作图.首先,我们先应该先了解一下什么是图,其次学习第一种图的算法,这种图 ...

  7. 一对矩阵的相关性_矩阵分析学习笔记(1)

    线性空间与线性映射 一.线性空间的概念: 记实数域 为 , 复数域为 , 统称数域 .设有一非空 集合记为 , 对集合 中的元素定义二元加法运算和数乘运算.(二元加法运算和数乘运算是一种线性映射,集合 ...

  8. 《SQL必知必会》第六课 用通配符进行过滤 使用LIKE操作符,%、[]、_通配符进行通配搜索

    第六课 用通配符进行过滤 使用LIKE操作符,%.[]._通配符进行通配搜索 #前面使用的所有操作符过滤中使用的值都是已知的 #利用通配符可以创建比较特定数据的搜索模式 #通配符:用来匹配值的一部分的 ...

  9. 网盘搜索_就用网盘传奇-最有效的百度网盘搜索引擎

    网盘搜索_就用网盘传奇-最有效的百度网盘搜索引擎 https://jidanso.com/

最新文章

  1. 重新分区_完全不需要装软件!教你轻松调整硬盘分区
  2. 将子目录分离(移动)到单独的Git存储库中
  3. JAVA 游览时间最长,[蓝桥杯][算法训练]景点游览-题解(Java代码)
  4. angular 模块构建_如何通过11个简单的步骤从头开始构建Angular 8应用
  5. Android中在SurfaceView上高效绘图
  6. KbmMW 4.30.00 发布
  7. ITIL 4和DevOps的关系?
  8. 妙用PRN文件,实现文档换机打印
  9. excel换行按什么键_4种方法,教你excel怎么自动换行
  10. 仿绚丽彩虹播放器程序源码
  11. spring gateway route超时时间原理解析和gateway调用流程
  12. 软件的接口设计图_重磅!PKPM 结构设计软件V5.1正式发布
  13. cdr文字内容显示不出来_cdr中字体预览不显示 字体安装后cdr不显示
  14. 笔记本HDMI1.4 1080p下外接高刷显示器的实现方法之一
  15. 使用python计算一年有多少秒_python获取一年所有的日期
  16. 上学期c语言作业答案,C语言作业答案4
  17. 仿 微信/QQ 实现小程序功能 -IOS
  18. 付宇泽20190919-4 单元测试,结对
  19. less和more的区别
  20. GBase 8m的高可用性详解

热门文章

  1. Vuex的第一次接触
  2. 使用vux组件库常见报错($t)处理
  3. 服务器端如何开启GZIP压缩功能
  4. js笔记(一)js基础、程序结构、函数
  5. NodeJS常用模块介绍
  6. (延迟两秒,跳转相应页面)(返回到上一个页面并刷新)
  7. linux cat显示若干行
  8. [LeetCode] Maximal Rectangle
  9. [置顶] Android的IPC访问控制设计与实现
  10. SVN+AnkhSVN端配置