Need Help about Ternary Search on integers

Правка en2, от Iron_Price, 2020-10-02 19:11:40

Is there anything wrong using this approach for ternary search on integers?

while (R - L >= 3)
{
  m1 = (L+L+R) / 3;
  m2 = (L+R+R) / 3;
  // say we're looking for the maximum
  if(f(m1) > f(m2)) R = m2;
  else if(f(m2) > f(m1)) L = m1;
  else L = m1, R = m2;
}
ans = inf;
for(i = L; i <= R; i++) ans = min(ans, f(i));

This blog shows a different way and tells not to use to approach above. But I don't understand why.

Теги ternary search

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский Iron_Price 2020-10-02 19:11:40 197
en1 Английский Iron_Price 2020-10-02 13:12:06 365 Initial revision (published)