当前位置:网站首页>关于二分一些模板
关于二分一些模板
2022-06-22 05:23:00 【hhyy_d】
1.一般的二分模板有两套,假设a数组是一个上升数组,待查找的为x。
//查找第一个出现的x,如果没有x则输出待插入的位置。
int search(int l,int r,int x)
{
while(l<r)
{
int mid = (l+r) / 2;
if(a[mid]>=x) r = mid;
else l = mid+1;
}
if(a[l]<x) return l+1;
else return l;
}
//在数组中查找最后出现出现的x,找到就返回x的位置,没有找到就返回x应该插入的位置。
int search1(int l,int r,int x)
{
while(l<r)
{
int mid = (l+r+1) / 2;
if(a[mid]<=x) l = mid;
else r = mid-1;
}
if(a[l] < x) return l+1;
else return l;
}
2.关于lower_bound函数和upper_bound函数
lower_bound函数是返回有序数组中第一个大于等于X的指针,不存在就返回end;
int a[] = {1,2,3,3,5};
lower_bound(a,a+5,3)-a得到2。
upper_bound函数返回有序数组中第一个大于x的指针
upper_bound(a,a+5,3)-a返回4。
边栏推荐
猜你喜欢

2022 refrigeration and air conditioning equipment operation test paper and refrigeration and air conditioning equipment operation test skills

加快推进工业互联网,图扑“智”绘发展新蓝图

Tidb performance overview panel

9. Gateway cross domain processing

Start with the strategy of small market value factor, and take you into quantitative investment (with the strategy of 77.83% annualized return)

When idea creates a method, it uses annotations to prompt method parameters (param), return value (return), and method function (description)

2022 welder (primary) new version test questions and welder (primary) free test questions

A piece of code to solve the problem of automatic disconnection of Google colab

Idea创建方法时,使用注解提示方法参数(param)、返回值(return)、方法作用(Description)

TIDB-performance overview面板
随机推荐
Lua notes
Hash type of redis
1108. Defanging an IP Address
rambbmitmq推送方
C language data type conversion rules (implicit conversion + explicit conversion)
Squoosh - 谷歌出品的免费开源图片压缩工具,图片大小减少90%!支持 API 开发调用
The difference between iqueryable and IEnumerable
部署SuperMap iServer war包时的服务迁移
Conversion between JSON, string and map
Kubernetes - deploy application to cluster
Solve the shortage of developers. Maybe it can help you
2022 a special equipment related management (elevator) examination data and a special equipment related management (elevator) analysis
DTS迁移秘籍-MySQL篇
Jedispool tool class
Monorepo silk slide methodology: reference module hot update
中小企业签署ERP合同时,需要留意这几点
新建本地内容上传至码云分支
新手开店货源怎么找,怎么找到优质货源?
在Vs Code中搭建JSP开发环境
【毕业季·进击的技术er】一个读研学生的唠唠嗑