Hi.
I was looking some solutions of problem 551C - GukiZ hates Boxes and I found this one: 11548616.
Basically, the code binary search the optimal time. One thing I can't understand is its stop condition (and then the choice of the optimal answer). The while stop condition in the main function is ma - mi > 1
, i.e., it will stop when mi + 1 = ma
and (I think) there will never be such case that mi = ma
. After that while, he prints the value of ma
(right 'pointer' of binary search).
My doubt is: why can't mi
be the optimal answer? How is he sure that ma
is already the optimal answer despite the fact that mi
is still inside the 'search' range?
Any help is appreciated. Thanks!
Because
mi
is actually never in the search range. It starts with an invalid value (0), and only receives invalid values from there.But really, when it comes to binary search, I guess the best thing is sticking with what works for you.
Oh, I see. You are right.
Thanks for the answer!