In a prison, there are
The room, in addition to the money bags, also contains a whiteboard.
A single number must be written on the whiteboard at any time.
Initially, the number on the whiteboard is
Then, the warden asks the prisoners to enter the room, one by one.
The prisoner who enters the room does not know which or how many other prisoners have entered the room before them.
Every time a prisoner enters the room, they read the number currently written on the whiteboard.
After reading the number, they must choose either bag
- Overwrite the number on the whiteboard with a non-negative integer and leave the room.
Note that they can either change or keep the current number.
The challenge continues after that (unless all
prisoners have already entered the room). - Identify one bag as the one that has fewer coins. This immediately ends the challenge.
The warden will not ask a prisoner who has left the room to enter the room again.
The prisoners win the challenge if one of them correctly identifies the bag with fewer coins.
They lose if any of them identifies the bag incorrectly, or all
Before the challenge starts, the prisoners gather in the prison hall and decide on a common strategy for the challenge in three steps.
- They pick a non-negative integer
, which is the largest number they may ever want to write on the whiteboard. - They decide, for any number
written on the whiteboard , which bag should be inspected by a prisoner who reads number from the whiteboard upon entering the room. - They decide what action a prisoner in the room should perform after getting to know the number of coins in the chosen bag. Specifically, for any number
written on the whiteboard and any number of coins seen in the inspected bag , they either decide- what number between
and (inclusive) should be written on the whiteboard, or - which bag should be identified as the one with fewer coins.
- what number between
Upon winning the challenge, the warden will release the prisoners after serving
Your task is to devise a strategy for the prisoners that would ensure they win the challenge (regardless of the number of coins in bag
Implementation Details
You should implement the following procedure:
std::vector<std::vector<int>> devise_strategy(int N)
: the maximum possible number of coins in each bag.- This procedure should return an array
of arrays of integers, representing your strategy. The value of is the length of array minus one. For each such that , the array represents what a prisoner should do if they read number from the whiteboard upon entering the room:- The value of
is if the prisoner should inspect bag , or if the prisoner should inspect bag . - Let
be the number of coins seen in the chosen bag. The prisoner should then perform the following action:- If the value of
is , the prisoner should identify bag as the one with fewer coins. - If the value of
is , the prisoner should identify bag as the one with fewer coins. - If the value of
is a non-negative number, the prisoner should write that number on the whiteboard. Note that must be at most .
- If the value of
- The value of
- This procedure is called exactly once.
Example
Consider the following call:
devise_strategy(3)
Let
- If
(including the initial number), inspect bag .- If it contains
coin, identify bag as the one with fewer coins. - If it contains
coins, identify bag as the one with fewer coins. - If it contains
coins, write on the whiteboard (overwriting ).
- If it contains
- If
, inspect bag .- If it contains
coin, identify bag as the one with fewer coins. - If it contains
coins, identify bag as the one with fewer coins. - If it contains
coins, write on the whiteboard (overwriting ). Note that this case can never happen, as we can conclude that both bags contain coins, which is not allowed.
- If it contains
To report this strategy the procedure should return
Constraints
Subtasks
- (5 points)
, the value of must not be more than . - (5 points)
, the value of must not be more than . - (90 points) The value of
must not be more than .
If in any of the test cases, the array returned by devise_strategy
does not represent a correct strategy, the score of your solution for that subtask will be
In subtask 3 you can obtain a partial score.
Let
Condition | Points |
---|---|
Sample Grader
The sample grader reads the input in the following format:
- line
: - line
: - last line:
Each line except the first and the last one represents a scenario.
We refer to the scenario described in line
The sample grader first calls devise_strategy(N)
.
The value of devise_strategy
does not conform to the constraints described in Implementation Details, it prints one of the following error messages and exits:
s is an empty array
: is an empty array (which does not represent a valid strategy).s[i] contains incorrect length
: There exists an index such that the length of is not .First element of s[i] is non-binary
: There exists an index such that is neither nor .s[i][j] contains incorrect value
: There exist indices such that is not between and .
Otherwise, the sample grader produces two outputs.
First, the sample grader prints the output of your strategy in the following format:
- line
: output of your strategy for scenario . If applying the strategy leads to a prisoner identifying bag as the one with fewer coins, then the output is the characterA
. If applying the strategy leads to a prisoner identifying bag as the one with fewer coins, then the output is the characterB
. If applying the strategy does not lead to any prisoner identifying a bag with fewer coins, then the output is the characterX
.
Second, the sample grader writes a file log.txt
in the current directory in the following format:
- line
:
The sequence on line
Attachment Package
The sample grader along with sample test cases are available here.
Comments