当前位置:网站首页>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;
}
边栏推荐
- 福州核酸检测实验室建设须知事项探讨
- 梅州植物组培实验室建设资料整理
- Exness: liquidity series - order block, imbalance (II)
- JVM(7):动态链接、方法的调用、四种方法调用指令区分非虚方法和虚方法、invokedynamic指令的使用
- Guanghetong launched a new generation of 3GPP R16 industrial 5g module fg160 engineering sample
- Statistical knowledge required by data analysts
- JVM (5): virtual machine stack, stack exception, stack storage result and operation principle, stack internal structure, local variable table
- Unity creates rivers on uneven terrain
- Production of unity scalable map
- JVM(5):虚拟机栈、栈异常、栈的存储结果和运行原理、栈内部结构、局部变量表
猜你喜欢

JVM (2): loading process of memory structure and classes

七个好用的装饰器

The future has come and the 5g advanced era has begun

Guanghetong launched a new generation of 3GPP R16 industrial 5g module fg160 engineering sample

【服务器数据恢复】同友存储raid5崩溃的数据恢复案例

Guanghetong advanced the fm160-cn project sample of the first 3GPP R16 industrial 5g module customized for China

Exness: liquidity series - order block, imbalance (II)

Redis主从复制、哨兵、cluster集群原理+实验(好好等,会晚些,但会更好)

MySQL stored procedure

Guanghetong officially released the annual theme of 2022 5g Huanxin: Five Forces co drive · Jizhi future
随机推荐
Data type conversion and conditional control statements
智慧工地怎样做到数字化转型?
AI helps release legal potential energy! Release of iterms contract intelligent review system
福州核酸检测实验室建设须知事项探讨
[server data recovery] data recovery case of RAID5 crash of buddy storage
Guanghetong launched a new generation of 3GPP R16 industrial 5g module fg160 engineering sample
正大国际:真正会在琪貨投资都都应该知道
Project architecture evolution
Guanghetong officially released the sc126 series of intelligent modules to promote more intelligent connection
Unity 在不平坦的地形上创建河流
Emlog新版导航源码/带用户中心
JVM (2): loading process of memory structure and classes
Production of unity scalable map
The live broadcast helped Hangzhou e-commerce Unicorn impact the listing, and the ledger system restructured the new pattern of e-commerce transactions
游戏数学: 计算屏幕点中的平面上的点(上帝视角)
谷歌的代码覆盖率最佳实践
Mathematical basis of information and communication -- the first experiment
codesys 获取系统时间
Commissioning experience and reliability design of brushless motor
Unity Editor Extension save location