For his wedding with Beatrice d'Este in 1491, the Duke of Milan Lodovico Sforza asked Leonardo to orchestrate the wedding celebrations, including a great jousting tournament that lasted for three whole days.
But the most popular knight is late...
Tournament
In a jousting tournament, the
knights are first arranged in a line and then their positions are numbered from
to
following the line order.
The joust master sets up a round by calling out two positions
and
(where
).
All the knights whose positions are between
and
(inclusive) compete: the winner continues in the tournament and goes back to his place in the line, whereas the losers are out of the game and leave the field.
After that, the remaining knights pack together towards the beginning of the line, preserving their relative order in the line, so that their resulting positions are from
to
.
The joust master calls out another round, repeating this process until just one knight remains.
Leonardo knows that all the knights have different strengths, represented as distinct ranks from
(weakest) to
(strongest).
He also knows the exact commands that the joust master will call out for the
rounds: he's Leonardo, after all... and he is certain that in each of these rounds the knight with the highest rank will win.
Late Knight
of the
knights are already arranged in the line, just the most popular knight is missing.
This knight has rank
and is arriving a bit late.
For the benefit of the entertainment, Leonardo wants to exploit his popularity and choose for him a position in the line that will maximize the number of rounds that the late knight will win.
Note that we are not interested in the rounds that don't involve the late knight, just in the rounds he takes part in and wins.
Example
For
knights, the
knights that are already arranged in the line have ranks
, respectively.
Consequently, the late knight has rank
.
For the
rounds, the joust master intends to call out the
positions per round, in this order:
,
,
.
If Leonardo inserts the late knight at the first position, the ranks of the knights on the line will be
.
The first round involves knights (at positions
,
,
) with ranks
,
,
, leaving the knight with rank
as the winner.
The new line is
.
The next round is
against
(at positions
,
), and the knight with rank
wins, leaving the line
.
The final round (at positions
,
) has
as the winner.
Thus, the late knight only wins one round (the second one).
Instead, if Leonardo inserts the late knight between those two of ranks
and
, the line looks like this:
.
This time, the first round involves
,
,
, and the knight with rank
wins.
The next starting line is
, and in the next round (
against
) the knight with rank
wins again.
The final line is
, where
wins.
Thus, the late knight wins two rounds: this is actually the best possible placement as there is no way for the late knight to win more than twice.
Statement
Your task is to write a program that chooses the best position for the late knight so that the number of rounds won by him is maximized, as Leonardo wants.
Specifically, you have to implement a routine called GetBestPosition(N, C, R, K, S, E)
, where:
is the number of knights;
is the number of rounds called out by the joust master (
);
is the rank of the late knight; the ranks of all the knights (both those already lined up and the late one) are distinct and chosen from
, and the rank
of the late knight is given explicitly even though it can be deduced;
is an array of
integers, representing the ranks of the
knights that are already on the starting line;
and
are two arrays of size
: for each
between
and
, inclusive, the
round called by the joust master will involve all knights from position
to position
, inclusive. You may assume that for each
,
.
The calls passed to this routine are valid: we have that
is less than the current number of knights remaining for the
round, and after all the
commands there will be exactly one knight left.
GetBestPosition(N, C, R, K, S, E)
must return the best position
where Leonardo should put the late knight
.
If there are multiple equivalent positions, output the smallest one.
(The position
is the
-based position of the late knight in the resulting line.
In other words,
is the number of other knights standing before the late knight in the optimal solution.
Specifically,
means that the late knight is at the beginning of the line, and
means that he is at the end of it.)
Subtask 1 [17 points]
You may assume that
.
Subtask 2 [32 points]
You may assume that
.
Subtask 3 [51 points]
You may assume that
.
Implementation Details
You have to submit exactly one file.
This file must implement the subprogram described above using the following signature.
Copy
int GetBestPosition(int N, int C, int R, int K[], int S[], int E[]);
This subprogram must behave as described above.
Of course you are free to implement other subprograms for their internal use.
Your submissions must not interact in any way with standard input/output, nor with any other file.
Sample Grader
The sample grader will expect input in the following format:
- line
:
,
,
;
- lines
:
;
- lines
:
,
.
Attachment Package
The sample grader along with sample test cases are available here.
Comments