[Dichotomy] binary search and use

Binary search

(Ps: Reference books: Challenges programming)

Find dichotomy: by shrinking the scope of the solution exists, in order to find a specific element of the array search algorithm.
Dichotomy is often seen in conjunction with other algorithms topics

Dichotomy of ideas:

(1) First, start the search from the middle of the array, if the value of the position is exactly the goal, then find, to end the search.

(2) if the first step is to search for a value greater than the target, put the array in half, in the array to the right to find the region, then repeating steps (1) operation.
If the first step of the searched value is less than the target, put the array in half, in the array on the left to find the region, then repeat steps (1).

(3) If the array is divided into a remaining elements still can not find, can not find it means.

Find a simple left to right, time is O (n), a binary search time of O (logn).

Usually written roughly as follows:

	//二分
	int l=0,r=n;		//	l:左边 r:右边 
	while(l<=r){
		int mid = (l+r)/2;
		if(a[mid]== k){
			cout<<mid;
			break;
		}
		if(a[mid] > k){
			r = mid-1;
		}else{
			l = mid+1;
		}
	}

Related to the use of

More than a good time, I wrote about are as above, L, R is the start array indices and tail index.

However, some problems, due to the nature of the subject, written above can not do this (I may have been too weak 55)
such that the following way:
Here Insert Picture Description
the usual kind of writing each index is to move forward or backward, is used in seeking a number which is present in the array
and this problem is to require a real number, and the value which does not appear in the array.
How to do that? Should be L, R is the range as the answer, and then with diethyl points requirements.
Write out something like this:

#include<iostream>
#include<cstdio>
using namespace std;
#include<cmath>
const int maxn = 10005;
int n,k;
double a[maxn];


bool f(double x){
	int cnt=0;
	for(int i=1;i<=n;i++){
		cnt += (int)(a[i]/x); 
	}
	return cnt>=k;
}
int main(){
	cin>>n>>k;
	double len = 0;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		len += a[i];
		
	}
	// 二分
	double l =0,r = len;
	for(int i=0;i<100;i++){
		double mid = (l+r)/2;
		if(f(mid)){
			l = mid;
		}else{
			r = mid;
		}
	}
//	cout<<l-1;
	printf("%.2f",l);
	return 0;
}

This dichotomy, L, R value is no longer a standard left, but the left and right range of the target answers,

Writing template:

// 二分
	int l =-1,r = n;
	while(r-l>1)
		int mid = (l+r)/2;
		if(a(mid) >=k){  //如果解满足条件,则解存在的范围变为(l,mid]
			r = mid;
		}else{
			l = mid;	 //如果解不满足条件,则解存在的范围变为(mid,r]
		}
	}
	cout<<r;

Maximizing the minimum range of issues

Similar to maximize the minimum, maximum minimize, maximize mean, you usually have to use a binary search,
such as the following this, other do not how to say it, roughly the same.
Here Insert Picture Description

#include<iostream>
#include<cstdio>
using namespace std;
#include<algorithm>
const int maxn = 100005;
int n,m;
int x[maxn];

bool f(int d){
	
	int last = 0;
	for(int i=1;i<m;i++){
		int k=last+1;
		while(x[k] - x[last] <d && k<n){
			k++;
		}
		if(k ==n){
			return false;
		}
		last = k;
	}
	return true;
}
int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>x[i];
	}
	sort(x,x+n);
	
	int l=0,r=1e8; 
	while(r-l>1){
		int mid = (l+r)/2;
		if(f(mid)){
			l = mid;
		}else{
			r = mid;
		}
	}
	cout<<l;
	return 0;
} 
Published 75 original articles · won praise 1 · views 3629

Guess you like

Origin blog.csdn.net/A793488316/article/details/105185602