当前位置:网站首页>[3. binary integer and floating point number]
[3. binary integer and floating point number]
2022-06-22 02:41:00 【Little silly bird_ coding】
1. Integer dichotomy
Dichotomous essence
- It's monotonous , It must be two points
- A dichotomous topic , It doesn't have to be monotonous
Ideas : There are two cases , There are two templates
Consider which template to use :
- Encounter dichotomy , finish writing sth. mid after , First write check function
- according to check(mid) Think about it ,true and false How to update the interval
- If the update interval is l = mid ,r = mid + 1 , At this time to use mid = (l + r + 1) / 2
- If the update interval is r = mid, l = mid + 1, At this time to use mid = (l + r) / 2
If in the first case ,l = r - 1, Because division is rounding down , therefore mid = l , At this time, the update interval is still [ l , r] Dead cycle .
subject
Given a length in ascending order is n Array of integers for , as well as q A query .
For each query , Returns an element k Starting and ending positions of ( Location slave 0 Start counting ).
If the element does not exist in the array , Then return to
-1 -1.
Input format
The first line contains integers n and q, Represents the length of the array and the number of queries .The second line contains n It's an integer ( Both in 1∼10000 Within the scope of ), Represents a complete array .
Next q That's ok , Each line contains an integer k, Represents a query element .
Output format
common q That's ok , Each line contains two integers , Represents the starting and ending positions of the desired element .If the element does not exist in the array , Then return to -1 -1.
Data range
1 ≤ n ≤ 100000
1 ≤ q ≤ 10000
1 ≤ k ≤ 10000
sample input :6 3 1 2 2 3 3 4 3 4 5sample output
3 4 5 5 -1 -1
Code
# include <iostream> const int N = 1e6 + 10; using namespace std; int n, m; int q[N]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i ++) scanf("%d", &q[i]); while (m --) { int x; scanf("%d", &x); int l = 0, r = n - 1; while(l < r) { int mid = (l + r) >> 1; if (q[mid] >= x) r = mid; else l = mid + 1; } if (q[l] != x) cout << "-1 -1"<< endl; else { cout << l << " "; int l = 0, r = n - 1; while (l < r) { int mid = (l + r + 1) >> 1; if (q[mid] <= x) l = mid; else r = mid - 1; } cout << l << endl; } } return 0; }
2. Floating point binary
- Floating point binary , It will be strictly reduced by half every time , Don't deal with boundary problems
- And integer bisection , Because there are integers , So the reduction is not necessarily half , Boundary issues need to be addressed
Ideas :
- Same as integer bisection , Only when r - l <= 1e6( Very small number ), At this point, there is no need for dichotomy
Code
Find the root X Value
#include <iostream> using namespace std; int main() { double x; cin >> x; double l = 0, r = x; while (r- l > 1e-8) { double mid = (l + r) / 2; if (mid * mid >= x) r = mid; else l = mid; } printf("%lf", l); return 0; }
3. Additional templates
Integer dichotomy
bool check(double x){ /* */} // Check x Whether it satisfies certain properties // Section [l, r] Divided into [l, mid] and [mid + 1, r] When using ; int bsearch_1(int l, int r) { while (l < r) { int mid = l + r >> 1; if(check(mid)) r = mid; //check() Judge mid Is the property satisfied else l = mid + 1; } return l; } // Section [l, r] Divided into [l, mid - 1] and [mid , r] When using ; int bsearch_2(int l, int r) { while (l < r) { int mid = l + r + 1 >> 1; if(check(mid)) l = mid; //check() Judge mid Is the property satisfied else r = mid - 1; } return l; }Floating point number template
bool check(double x){ /* */} // Check x Whether it satisfies certain properties double bsearch_3(double l, double r) { while (l < r) { double mid = l + r >> 1; if(check(mid)) l = mid; //check() Judge mid Is the property satisfied else r = mid ; } return l; }
边栏推荐
- Database daily question - day 19: top ranked travelers
- 【Docekr学习遇到的坑】
- Graphacademy course explanation: introduction to neo4j graph data science
- cmake常用命令分类备忘
- FPGA-Xilinx 7系列FPGA DDR3硬件设计规则
- June25,2022 PMP Exam clearance manual-4
- Dernière publication: neo4j Graph Data Science GDS 2.0 et aurads ga
- MATLAB 学习笔记(4)MATLAB 数组
- Anaconda historical version download
- 【1. 快速排序】
猜你喜欢

Using hook based on xposed framework

Architecture and practice of vivo container cluster monitoring system

How to select the appropriate version of neo4j (version 2022)

EMC Radiation Emission rectification - principle Case Analysis

Wechat applet film and television comment exchange platform system graduation design (2) applet function

EMC整改小技巧

Rational Rose installation tutorial

小孩子学什么编程?

GraphAcademy 课程讲解:《Neo4j 图数据科学基础》

Word document to markdown document?
随机推荐
微软 IE 浏览器于 6 月 15 日被永久关闭
GraphAcademy 课程讲解:《Neo4j 图数据科学简介》
并查集dsu
Minecraft 1.18.2 生化8 模组 1.3版本 物品3D化+更加复杂村庄
Get to know unity3d (project structure, third-party plug-in of probuilder)
The "cloud" end of the 6th world intelligence conference will be held soon
关于PMP考试,你想知道的知识都在这里了
2022 brazing test simulation 100 questions and answers
【9. 子矩阵和】
C# 判断应用是否启动并展示
Transformation numérique des RH avec okr
PMP备考相关敏捷知识
Write your own kubernetes controller
Latest release: neo4j figure data science GDS 2.0 and aurads GA
ModuleNotFoundError: No module named ‘torchvision. datasets‘; ‘ torchvision‘ is not a package
When retail digitalization enters a new stage of development, we need to connect the public domain with the private domain
【Docekr学习遇到的坑】
Technical exploration: 360 digital subjects won the first place in the world in ICDAR OCR competition
Kubernetes code generator use
EMC radiation emission rectification - principle case analysis

