Editorial for IOI '09 P1 - Archery
Submitting an official solution before solving the problem yourself is a bannable offence.
The proofs below are very verbose and long-winded, but the ideas behind the algorithms are not all that complicated. The steps can be summarized as:
- A trivial solution (trying all possibilities and simulating the tournament for each one) gives an
algorithm.
- One can observe that after no more than
rounds, the tournament becomes cyclical with all positions repeating every
rounds. This allows the trivial algorithm to be sped up to
.
- One can optimize the simulation of the tournament (once we have chosen an initial position) from
to
. This is the most complicated part of the solution. The key here is that we are only interested in our own position at the end, not in everyone else's.
- Once you have a subroutine that tells you where you will end up given where you start, you can use it with a modified binary search, improving the
algorithm to
; or alternatively, improving the
algorithm to
.
- The last two algorithms above also have slower versions (
and
) if you try to solve the problem by also keeping track of other archers' positions, not just your own.
Optimising to 
A trivial solution is to try all possible starting positions and simulate the tournament for each round, giving complexity of . We now show how this can be reduced to
.
Consider the archers numbered from to
. Let's call them the weak archers.
Theorem 1. After enough rounds (no more than ) the weak archers will occupy the targets numbered
to
, one such archer on each target, and will stay there until the end of the tournament.
Proof. After rounds, archer number
will be on target
and will stay there until the end. From this point on, if we consider the set of
archers consisting of archer number
plus the
weak archers (let us call this the weak+1 set), and if we imagine the targets arranged in a circle (
to
and then again
), we have the following scenario:
- When an archer from the weak+1 set competes with an archer outside of weak+1, then the weak+1 archer will stay on the target and the other archer will move.
- When two archers belonging to the weak+1 set compete against each other, one of them will stay and the other will move to the target on his left.
Lemma 1. Within rounds after archer number
has arrived on target
, every target will have at least one weak+1 member on it.
Proof. Suppose the above is not true. We know that once a target is occupied by a weak+1 member, then it will always be occupied by at least one (because weak+1 members never move out of their target unless there is another weak+1 archer to replace them there). Thus if Lemma 1 is false, there must exist a target that is never occupied by a weak+1 member (within the rounds). Let's call this target
. If
is never occupied by a weak+1 archer, then the target to the left of
(let us call it
) would have at most one such archer within one round and would remain this way. Then within two rounds the target to the left of
would have at most one weak+1 archer, and within three rounds the next target to the left would have at most one such archer. Continuing around the circle, within
rounds the target to the right of
would have at most one weak+1 archer. Thus within
rounds, all targets except
would have at most one weak+1 archer. But since there are
such archers and
targets, this means that
must have at least one weak+1 archer. Since this contradicts our supposition that
remains free of weak+1 archer for
rounds, this proves Lemma 1.
Now that we know every target has at least one weak+1 archer within rounds from the start of the competition, and since we know that once a target has such an archer it never ceases to have at least one, we have proved Theorem 1.
Now consider all archers that don't belong to weak+1. If we have one weak+1 archer on every target, this also means we also have one non-weak+1 archer on every target. Since under this scenario the weak+1 archers always stay where they are, this means the archers numbered to
will cyclically rotate around the
targets, periodically repeating their positions after every
rounds.
This means that if we replace by
we would get an identical answer, since the outcome of the tournament after
rounds would be identical to the outcome after
rounds (remember that
).
The above means that we can easily improve our algorithm to
.
Optimising to 
Currently, when we choose a starting position and we simulate what happens after rounds, we do
calculations per starting position. We can reduce the complexity of this part to
in the following way.
Observe that there are three types of archers: ones that are better than us, which we'll call the black archers; ones that are worse than us, which we'll call the white archers; and ourself (a single archer) which we'll denote as the gray archer. In order to solve our problem, we need not make any distinctions between archers of the same colour, as it is irrelevant to the final outcome. If two archers of the same colour compete against each other, it does not matter to us which one prevails (i.e., it is not going to impact the gray archer's final position). And we know that whenever two archers of different colours compete against each other, the archer of the darker colour wins.
Now there are three different cases which we'll handle separately.
Case 1 There are no black archers. This means we are the best archer and in this case it is trivial to show that the optimal target to start on is target .
Case 2 There is at least one black archer, but no more than . This means that our rank is between
and
, which means we are part of the group of archers that eventually ends up circling the targets periodically. In this case, it is notable that we do not need to simulate the full tournament, but only what happens on target
. If we know who competes on target
every round, then just observing when between rounds
and
the gray archer gets to compete against archer number
will tell us where the gray archer will finish the tournament (which is all that we are interested in). We will track what happens on target number
using the following algorithm:
We assign each archer a number
, which informally speaking indicates the earliest possible round where
might potentially compete on target
. Initially each archer's
number equals his original target number. Then we simulate each round of the tournament with the following procedure:
- We determine who is competing on target
. The first archer there is clearly the winner on target
from the previous round (or initially the leftmost archer). We determine his opponent in the following way. We take all archers with
number less than or equal to the number of the current round. The best archer among them will now be competing on target
(the proof of why this is correct is further below).
- We compare these two archers and assign the loser a
value equal to the number of the current (just passed) round plus
. This is the earliest round when we might potentially see this archer back on target
.
Now let us prove that the above is correct. We will denote with the archer who is competing on target
on round
, but who was competing on target
on round
. Every archer
has a value
that if he were to win every single contest since getting that
value, he would end up being
. Now let's look at the archer selected by our algorithm to be
(for some round
). We will denote him by
. Let
. If
is zero, this means that
didn't have the opportunity to become
even if he were to win all his contests. Hence, in this cycle
never met
(or any of the earlier
's). Since
never met these archers and since he is better than everybody else who is in the running for
, this means that he never lost in this cycle (until he got to target
at least), which means he truly is
(i.e., our algorithm is correct in this case).
If is greater than zero, this means that
had the opportunity to become
, but lost it. This means he competed directly with
because the latter was the only candidate for
that was better than
2. Now if
competed with
and if he is better than every other
candidate, this means that after their meeting
was always "on the heels" of
: either on the same target, or on the one immediately to the right. This means that when
reached target
(which is in round
),
was on target
. Since by definition he was better than the other archer on target
, this means he was indeed the one to reach target
on round
.
2 This is true because by our algorithm every candidate for automatically becomes a candidate for
, except for the actual
— so if
was an
candidate, but did not succeed and is now the best among the
candidates, he must have been second to
among the
candidates.
Now that our algorithm for keeping track of target is proved correct, we can analyze its time complexity. Since we make no distinction between same-coloured archers, we can represent any set of archers by just three numbers: the number of white, gray and black archers in that set. This allows us to execute every step of the algorithm (i.e., every round of simulation) in constant time, because all we have to do is determine the colour of the best archer in a set of candidates and then add to that set only one or two new archers (those whose
value equals the number of the next round). Since we only need to simulate up to round
, and we are not interested in
values above
, we can implement our simulation algorithm in
time and space.
Case 3 There are more than black archers. This means our number is more than
, which means that we are one of the archers that eventually end up standing on the same target indefinitely. We only need to determine which target that is.
We already showed that once archer arrives on target
, all that the weak+1 archers do is push each other around the targets until they settle on a different target each. Since our number is greater than
, this means that all white and gray archers belong to the weak set. Thus all we need to do is simulate how the white and gray archers push each other around. We start at target
where we know no white/gray archer would be allowed to stay. Then we attempt to count how many white/gray archers end up getting "pushed" around the circle after every target. Initially the white/gray archers pushed from
to
would be those that were initially at target
(note that our count is still a lower bound; later on we may find out there were even more white/gray archers pushed from target
). Then we move to target
. We add any white/gray archers that start there to the ones we transferred from target
and we leave one of the combined set there (we always leave a white one, if we have one; if not, we leave the gray; if the set is empty, then we obviously do not leave anyone and let the black archers have this spot). We keep going around the circle from
to
, to
, etc. On every target, we "pick up" any white/gray archers and we leave one of those we have picked up either earlier or now. Eventually we get to target
and if we happen to have any white/gray archers pushed to target
, we just transfer them to target
and keep going with the same procedure. The second time we return to target
we certainly will not have any more white/gray archers to push around, because by Theorem 1 we know that in
rounds every white or gray archer would have settled on a target. This algorithm clearly runs in linear time and space for the same reasons as the algorithm in Case 2 above. It is also correct because we only move around white/gray archers when necessary (i.e., when they would end up on the same target with another white/gray archer or on target
) and we make sure that in the end every white/gray archer would have settled somewhere where he can remain undisturbed until the end of the tournament.
The optimization of the tournament simulation from to
described above improves our solution to the whole problem from
to
.
Optimising to 
The last optimization that we use to bring the complexity of our algorithm down to is based on the well-known technique of binary search. The efficient tournament simulation algorithms described above can easily be modified to also tell us how many times the gray archer moves from target
to target
(denoted by
). Combining this information with the final position of the gray archer (denoted
) allows us to view the final position on a linear (as opposed to circular) scale. If we describe the outcome of a simulation as being the number
we can think of every transfer of the gray archer from one target to another as decrementing the outcome by one. Then if we look at the simulation algorithms described above, we can observe that if the starting position is higher, then the final outcome can never be lower. For example, if you choose to start with a larger
value this can never get you further ahead (on the linear scale, not the circular) than if you had chosen a smaller initial
value.
Given this monotonic relationship between the starting position and the final outcome of a tournament, can find the optimal starting position as follows:
- Measure the outcome of starting on target
.
- Measure the outcome of starting on target
.
- For each multiple of
in this range, use standard binary search to find the smallest possible ending position strictly greater than this multiple (and hence the closest to target
for a particular number of wrap-arounds).
- Of the starting positions found above, pick the best.
Since there are only rounds being considered, the range to search is
and hence only
binary searches are required. Each such binary search requires
starting positions to be tested, giving a time complexity of
.
Additional notes
Finally, we should note that the efficient simulation algorithms described above (which ignore distinctions between same-coloured archers and work in time per simulation) can be implemented in a way that does distinguish between the different black or white archers, using binary heaps or other similar data structures. This would give a final algorithm of complexity
or
, depending on whether one also uses the binary search. One can also achieve a time complexity of
or
by applying the binary search technique without optimizing the simulation.
It is also possible to solve the problem in linear time, but this is very difficult and left as an exercise to the reader. An solution is sufficient to receive a full score.
Comments