Tennis tournament P73160


Statement
 

pdf   zip   main.cc

Consider a tennis tournament with mm participants with names x1,x2,,xmx_1, x_2, \dots, x_m, where mm is a power of two. In the first round x1x_1 plays against x2x_2, x3x_3 plays against x4x_4, …, and xm1x_{m - 1} plays against xmx_m. The players that lose are eliminated, and the process is repeated with the remaining players. When only a player remains, this is the winner of the tournament.

For instance, let m=8m = 8, and suppose that in the first round x1x_1 defeats x2x_2, x3x_3 loses against x4x_4, x5x_5 defeats x6x_6, and x7x_7 defeats x8x_8. In the second round x1x_1 plays against x4x_4 (suppose that x4x_4 wins), and x5x_5 plays against x7x_7 (suppose that x7x_7 wins). In the third and last round x4x_4 plays against x7x_7. Assuming that x4x_4 wins, this is the champion. The following figure shows the championship just described:

image

Write a procedure that, given the names of the players and the table of results, returns the name of the winner of the tournament:

    string winner(const vector<string>& name, const vector<bool>& win);

The vector @name@ has size mm, where mm is any power of 2. For each @j@ between 0 and m1m - 1, we have @name[j]@ =xj+1= x_{\mbox{j} + 1}. All the names are different.

For instance, this could be the table of names of the tournament previously described:

image

The vector @win@ has size m1m - 1 and contains all the results of the matches: the first round is stored in the last m/2m/2 positions, the second round is stored in the m/4m/4 previous positions, the third round is stored in the m/8m/8 previous positions, …, and the result of the last round (the final) is stored in @win[0]@. The boolean of each position indicates if the first player (the one with the smallest index) has won against the second one.

This would be the table of results of the tournament previously described:

image

For the example tournament, the answer should be “Borg”.

Observation

You only need to submit the required procedure; your main program will be ignored.

Information
Author
Salvador Roura
Language
English
Translator
Carlos Molina
Original language
Catalan
Other languages
Catalan
Official solutions
C++
User solutions
C++