NOTE: This problem statement contains an image that may not display properly if viewed outside of the applet.
You have written several chess programs and want to have a tournament with them. All your programs have ratings, which entirely determine the result of each game (the player with a higher rating always wins).
Before the tournament all programs are placed in a list. You want to put yourself somewhere in this list (the total number of participants including you will be a power of 2). In every round, the first participant in the list plays the second, the third plays the fourth, and so on. Players who lose their games are eliminated and removed from the list, while the winners advance to the next round. In the next round the process is repeated with the list of winners. The tournament lasts until only one player (the winner of the tournament) is left.
For example, if players participating in the tournament have ratings {100, 300, 200, 150}, then Player 1 plays Player 2 and Player 3 plays Player 4. Players 2 and 3 win and meet in the final, where Player 2 wins.
You want to win the tournament at any price, so you enter a game winning cheat code whenever you lose. However, you want to cheat as rarely as possible. You need to find the place in the initial list which allows you to win the tournament with as few cheat codes as possible. You will be given the ratings of all the programs, in the order they are placed in the list. Given yourRating as well, return the minimal number of codes you must enter to win the tourney.
|