Home > Back-end >  Binary algorithm for the answers
Binary algorithm for the answers

Time:10-05

Is legal in the middle of the binary binary answer why don't we talk about the title of the mid value will change, sometimes is mid=(L + R)/2, is sometimes mid=(L + R + 1)/2;
What good method can be well understood this boundary? Or is there a better template
I use the template is like this:
Max:
 void of solve () {
Int l=0, r=MAXN;
for(int i=0; i<100; I++) {
Int mid=(r + l + 1)/2;//this place what determine
If (check (mid)) {
L=mid;
} else {
R=mid - 1;
}
}
Cout}

A minimum value:
 void of solve () {
Int lb=0, rb=maxn;
for(int i=0; i<100; I++) {
Int mid=(lb + rb + 1)/2;//this place what determine
If (check (mid)) {
Rb=mid;
} else {
Lb=mid + 1;
}
}
Cout}

  • Related