一、二分查找
其本质就是找一个区间
二、整数二分
2.1. 查找左边界的模板
int findPrior(int left,int right,int target) {
while (left < right) {
int mid = (left + right) / 2;
if (a[mid] >= target) right = mid;
else left = mid + 1;
}
if (a[left] == target) return left;
else return false;
}
2.2. 查找右边界模板
int findTail(int left, int right, int target) {
while (left < right) {
int mid = (left + right) / 2;
if (a[mid] <= target) left = mid;
else right = mid - 1;
}
if (a[left] == target) return left;
return false;
}
2.2. 相关题目
#include<stdio.h>
int main() {
int n;
int q;
int k;
int a[100010];
scanf("%d %d", &n, &q);
for (int i = 0; i < n; i++)
scanf("%d", a + i);
while (q--) {
scanf("%d", &k);
int left = 0;
int right = n;
while (left < right) {
int mid = (left + right) / 2;
if (a[mid] >= k) right = mid;
else left = mid + 1;
}
if (a[left] != k)
printf("-1 -1\n");
else {
printf("%d ", left);
left = 0; right = n - 1;
while (left < right) {
int mid = (left + right + 1) / 2;
if (a[mid] <= k) left = mid;
else right = mid - 1;
}
printf("%d\n", left);
}
}
}
三、浮点数二分
3.1. 相关题目
#include<stdio.h>
#include<math.h>
int main(){
double n;
scanf("%lf",&n);
double left = -10000,right = 10000;
while (right - left >= 1e-8) {
double mid = (left + right) / 2;
if (mid * mid * mid <= n) left = mid;
else right = mid;
}
printf("%.6f",left);
return 0;
}
标签:二分,right,int,mid,while,查找,left
From: https://www.cnblogs.com/cony1/p/17815833.html