当前位置:网站首页>lower_bound,upper_bound,二分
lower_bound,upper_bound,二分
2022-06-11 04:32:00 【superkcl2022】
#include <iostream>
using namespace std;
/** * lower_bound 若有多个等于target的数,返回最左边的那个数,否则返回 > target的那个数的下标 * 如果 tar最小返回0,如果tar最大返回数组最大值下标+1 * * * 1. target的值比cnt[0]小,while循环直到 l == r == 0, a[0] >= tar 返回0 * 2. target最大, 会一直执行这条语句 a[mid] < tar ,index的值不变 * */
/** * 二分,log级别,未查询到叶子节点,即可停止,而lower_bound停不下来,一直到叶子节点 * */
int lower_bound(int a[],int l,int r,int tar){
int index = r+1;
while(l <= r){
int mid = l + (r-l)/2;
if(tar > a[mid]) l = mid + 1; //中间值小了
else {
// a[mid] == tar时,r = mid - 1
index = mid;
r = mid - 1;
}
}
return index;
}
int upper_bound(int a[],int l,int r,int tar){
int index = r+1;
while(l <= r){
int mid = l + (r-l)/2;
if(tar >= a[mid]) l = mid + 1;
else{
index = mid;
r = mid - 1;
}
}
return index;
}
int main() {
int cnt[] = {
2,4,4,5,7,8,9};
cout << lower_bound(cnt,0,sizeof(cnt)/sizeof(int)-1,14) << endl;
cout << upper_bound(cnt,0,sizeof(cnt)/sizeof(int)-1,4) << endl;
return 0;
}
边栏推荐
- Redis持久化(少年一贯快马扬帆,道阻且长不转弯)
- Commissioning experience and reliability design of brushless motor
- 精益产品开发体系最佳实践及原则
- Detailed decomposition of the shortest path problem in Figure
- MySQL stored procedure
- PCB ground wire design_ Single point grounding_ Bobbin line bold
- Check the digital tube with a multimeter
- Unity MonoSingleton
- Unity 可缩放地图的制作
- PostgreSQL数据库复制——后台一等公民进程WalReceiver 收发逻辑
猜你喜欢

碳路先行,华为数字能源为广西绿色发展注入新动能

Unity 伤害值的显示

Guanghetong LTE Cat4 module l716 is upgraded to provide affordable and universal wireless applications for the IOT industry

Guanghetong LTE CAT6 module fm101-cg, which supports CBRS band, took the lead in obtaining FCC certification

游戏数学: 计算屏幕点中的平面上的点(上帝视角)

It's 2022. When will the "module freedom" be realized?

Unity 编辑器扩展 保存位置

JVM (5): virtual machine stack, stack exception, stack storage result and operation principle, stack internal structure, local variable table

一款自适应的聊天网站-匿名在线聊天室PHP源码

Unity 遮挡剔除
随机推荐
Carbon path first, Huawei digital energy injects new momentum into the green development of Guangxi
Description of construction scheme of Meizhou P2 Laboratory
Unity 消息框架 NotificationCenter
数字电影的KDM是什么?
Product milestones in May 2022
Unity Advanced Backpack System
JVM (4): active and passive use of classes, internal structure of runtime data area, JVM thread description, PC register
Redis持久化(少年一贯快马扬帆,道阻且长不转弯)
Analysis of hidden dangers in the construction of Fuzhou chemical laboratory
Guanghetong advanced the fm160-cn project sample of the first 3GPP R16 industrial 5g module customized for China
L'avenir est venu, l'ère 5G - Advanced s'ouvre
Unity item model rotating display
Record an ES accident
Programming Examples Using RDMA Verbs
The second discussion class on mathematical basis of information and communication
AI helps release legal potential energy! Release of iterms contract intelligent review system
Guanghetong LTE CAT6 module fm101-cg, which supports CBRS band, took the lead in obtaining FCC certification
新UI学法减分专业版34235道题库学法减分专业版小程序源码
记一次ES 事故
Use tool classes to read Excel files according to certain rules