Home > Back-end >  Valley of problem solving, binary search
Valley of problem solving, binary search

Time:02-06

Below is the answer key, why this one can use binary search? Not can be used to sort good series? I see don't know much about the realization of binary search for solutions
#include
using namespace std;
Long long n, bz, s=0, mid, leftt, longest, trees [1000008].
Int main ()
{
The scanf (" % LLD % LLD, & amp; N, & amp; Bz);
for(int i=1; i<=n; I++)
{
The scanf (" % LLD, & amp; Trees [I]);
Longest=Max (longest, trees [I]);//find the longest wood
}
While (leftt<=longest)
{
Mid=(leftt + longest)/2;//start from the middle point as the logging machine height
s=0;
for(int i=1; i<=n; I++)
If (trees [I] & gt; Mid)//height of the tree is greater than the wood cutter height
S +=trees [I] - mid;//high part of the accumulative
If (sLongest=mid - 1;//on the left side search reduce height increase wood
The else
Leftt=mid + 1;//on the right search increase highly reduce the wood
}
Coutreturn 0;
}

CodePudding user response:

The binary search is not from the binary trees, but the height of the binary, namely find maximum height, so leftt from 0 to the maximum height is this order is?

CodePudding user response:

reference 1/f, confident boy reply:
does not spring from the binary search trees, but the height of the binary, or find a maximum height, so leftt from 0 to the maximum height is this order is?

Oh oh is this meaning, why finally return to leftt - 1? Why the while condition is letff <=longest?
There are many details is not very good, such as its range

CodePudding user response:

reference 1/f, confident boy reply:
does not spring from the binary search trees, but the height of the binary, or find a maximum height, so leftt from 0 to the maximum height is this order is?

And while loop body why s=0