Answer the question
In order to leave comments, you need to log in
What is the algorithm for solving the problem of beetles that do not like to be close to each other?
Hello. I ask you to help with the solution of the educational problem, because I do not really understand the essence of the problem and therefore do not know where to start.
The task is as follows:
Beetles do not like to be near each other and each one hides under a separate stone and tries to choose stones that are as far away from their neighbors as possible. Also, beetles like to be as far away from the edge as possible. Once a beetle sits behind a rock, it no longer moves. In total there are X stones in the line. And there sequentially runs to hide Y beetles. Find how many free stones will be left and right of the last beetle. X can be up to 4 billion.
Examples:
X=8, Y=1 - answer 3.4
X=8, Y=2 - answer 1.2
X=8, Y=3 - answer 1.1
Answer the question
In order to leave comments, you need to log in
Everything is simple here, each next beetle will look for a point equidistant from the edge and other beetles.
that is, the first one will sit exactly in the middle, the next two will sit on both sides of the first, in the middle between the first and the edges, and so on.
In fact, this is a task of finding the values of the fractal.
In general, in this form, the problem may not have an unambiguous solution.
The first example X=8, Y=1 gives two solutions - (3,4) and (4,3).
Here, of course, there are already three approaches:
1. Simulate the situation of seating beetles, although the remark about 4 billion hints that this is not what is expected of us. And if tomorrow there will be 4 trillion?
2. You can see that the distance, for example, of the left beetle (when seated from left to right) from the edge of a segment of length X, depending on the number of beetles, is a series: L(1) = X/2, L(2) = X/4, L( 3) = X/4, L(4) = X/8, L(5) = X/8, L(6) = X/8, L(7) = X/8... etc. It remains to figure out what kind of formula is hiding here. Power of two? Pascal's triangle? It's getting warmer. Oh, why does a programmer need a higher education? No, it's not necessary!
3. About fractals is true, in terms of calculations it means recursion. The algorithm may come out elegant, but it hoards from the hordes of beetles faster than option 1.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question