当前位置:网站首页>Class notes (5) (1) - 593. Binary search
Class notes (5) (1) - 593. Binary search
2022-07-28 02:21:00 【xyc20120615】
Description
Give a yes n A sequence of elements from small to large , Please program to find the position where an element first appears .(n≤10^6)
Format
Input
first line : An integer , Indicates the number of sequential elements from small to large ;
below n That's ok , One integer per row ;
The last line is an integer x, Represents the element to be found ;
Output
If x In the sequence , The output x First occurrence , Otherwise output -1.
Samples
input data 1
5
3
5
6
6
7
6
Output data 1
3
Limitation
1s, 1024KiB for each test case.
Method 1 :
Look at this name, you know it needs two points , But I don't believe in evil , Tried the violent search , actually AC 了 !, I have to make complaints about it , that OJ The data is real water .
Program :
#include<bits/stdc++.h>// This procedure is not recommended , It is likely to explode
using namespace std;
int n,x,a[1000010];
int main(){
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
cin>>x;
for(int i=1;i<=n;i++)
if(a[i]==x){
cout<<i;
return 0;
}
coutt<<-1;
return 0;
}Method 2 :
Is the normal binary search , The general idea is to open the setting range of two variables, and then reduce the range according to the value between the variables, and find it on paper , Note that only when the array is ordered can it be bisected , Or sort it first .
Program :
#include<bits/stdc++.h>
using namespace std;
int n,x,a[1000010];
int main(){
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
cin>>x;
int l=1,r=n;
while(l<=r){
int mid=(l+r)/2;
if(a[mid]<x)
l=mid+1;
else
r=mid-1;
}
if(a[l]!=x){
cout<<-1;
return 0;
}
cout<<l;
return 0;
}PS: Method 1 please use it carefully !
边栏推荐
- 考研数学一元微分学证明题常见题型方法
- 测试/开发程序员的级别“陷阱“,级别不是衡量单维度的能力......
- [深入研究4G/5G/6G专题-42]: URLLC-14-《3GPP URLLC相关协议、规范、技术原理深度解读》-8-低延时技术-2-基于slot的调度与Slot内灵活的上下行符号配比
- go 学习02 基础知识
- Five basic data structures of redis
- 对话Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
- 【ROS进阶篇】第十讲 基于Gazebo的URDF集成仿真流程及实例
- 软工必备知识点
- 视频常用分辨率
- feign调用get和post记录
猜你喜欢

记录一次生产死锁

LeetCode 热题 HOT 100 -> 1.两数之和

How to evaluate the effectiveness of R & D personnel? Software Engineer reports help you see everyone's contribution

Vxe Table/Grid 单元格分组合并

样本不均衡-入门0

The cooperation between starfish OS and metabell is just the beginning

Go learning 01

Promise from introduction to mastery (Chapter 2 understanding and use of promise)

小米网站主页面大模块——小模块+导航(浮动案例)

How to put app on the app store?
随机推荐
Promise from introduction to mastery (Chapter 4 async and await)
对话Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
Two ways for wechat applet to realize dynamic horizontal step bar
LeetCode 热题 HOT 100 -> 1.两数之和
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
Software testing interview question: why should we carry out testing in a team?
SFTP file / folder upload server
54:第五章:开发admin管理服务:7:人脸入库流程;人脸登录流程;浏览器开启视频调试模式(以便能够在本机的不安全域名的情况下,也能去开启摄像头);
一文读懂Plato&nbsp;Farm的ePLATO,以及其高溢价缘由
实际工作中,我是如何使用 Postman 做接口测试?
软考 --- 数据库(2)关系模型
视频常用分辨率
Promise from introduction to mastery (Chapter 2 understanding and use of promise)
Ceresdao: new endorsement of ventures Dao
Data output - image annotation and annotation
Promise从入门到精通(第4章 async 和 await)
C#引入WINAPI传递中文字符串参数字符集问题
Flex布局—固定定位+流式布局—主轴对齐—侧轴对齐—伸缩比
Codeforces Round #807 (Div. 2) A-C题解
Codeforces Round #810 (Div. 2)A~C题解