S
S
Serufim2017-11-05 19:08:48
Java
Serufim, 2017-11-05 19:08:48

How to solve the problem of Finding a boundary element?

There is the following task, if it is not difficult to write an algorithm for finding F with a speed of lg N and everything is solved by ordinary binary search, then I can’t think of an algorithm capable of solving such a problem in lg F, just like an algorithm that would solve the problem in 2 √N. I don't need a code, just please tell me where to start and how to approach the implementation of these algorithms
===== The task itself =====
There is a building with N floors and an unlimited number of eggs. Will consider an egg to be broken if it falls from floor F or higher. If the egg falls from the floor below, then it remains intact and can be reused. It is required to implement an algorithm for finding the value of F, provided that it is possible to break approximately lg N eggs and perform approximately lg N attempts.
Modify the search algorithm so that the number of attempts is about lgF, provided that N is much greater than F.
Optional. Solve the problem, provided that there are only two eggs. Implement the search algorithm F with the number of attempts not exceeding 2(√N). Try to reduce the search cost to C(√N) for some constant C.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
youngmysteriouslight, 2017-11-05
@Serufim

If you test floors 1, 2, 4, 8, ..., 2^K, 2^{K+1} in turn until the egg breaks, and then refine the value of F from the interval 2^K..2^ {K+1} binary search, the number of attempts will be (2 lgF). If the word "about" allows a factor of 2, then the algorithm fits.
If there is only one egg available, then there is no other way than to go from bottom to top in a row, O(N).
If there are two eggs, the first one can go with a coarser step A[1], A[2], A[3], ..., A[K], A[K+1], and the second one can refine the exact value of F from A[K]..A[K+1]. Moreover, the step A[K+1]-A[K] determines the number of attempts for the second egg, that is, the total number of attempts will be K+A[K+1]-A[K]. It remains to determine the form of the sequence A[i] so that K+A[K+1]-A[K]=O(√A[K+1]).
Actually, to reach 2(√N), you need to go with the first egg in steps (√N), that is, 1, 1+(√N), 1+2(√N), 1+3(√N), .. ., and when it splits into 1+(K+1)(√N), go second in a row from the floor 2+K(√N).

D
Denis Zagaevsky, 2017-11-05
@zagayevskiy

General solution https://habrahabr.ru/post/211200/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question