In a prison, there are prisoners.
One day, the warden offers them a chance to free themselves.
He places two bags with money, bag
and bag
, in a room.
Each bag contains between
and
coins, inclusive.
The number of coins in bag
is different from the number of coins in bag
.
The warden presents the prisoners with a challenge.
The goal of the prisoners is to identify the bag with fewer coins.
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 or bag
.
The prisoner then inspects the chosen bag, thus getting to know the number of coins inside it.
Then, the prisoner must perform either of the following two actions:
- 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 of them have entered the room and not attempted to identify the bag with fewer coins.
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 more days.
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 and bag
).
The score of your solution depends on the value of
(see Subtasks section for details).
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 denote the number the prisoner reads from the whiteboard upon entering the room.
One of the correct strategies is as follows:
- 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 .
The length of the returned array is
, so for this return value the value of
is
.
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 be the maximum value of
for the returned arrays over all test cases in this subtask.
Your score for this subtask is calculated according to the following table:
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 as scenario
.
In scenario
bag
contains
coins and bag
contains
coins.
The sample grader first calls devise_strategy(N)
.
The value of is the length of the array returned by the call minus one.
Then, if the sample grader detects that the array returned by
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 indexsuch that the length of
is not
.
First element of s[i] is non-binary
: There exists an indexsuch that
is neither
nor
.
s[i][j] contains incorrect value
: There exist indicessuch 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 character
A
. If applying the strategy leads to a prisoner identifying bagas the one with fewer coins, then the output is the character
B
. 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 corresponds to scenario
and describes the numbers written on the whiteboard.
Specifically,
is the number written by the
prisoner to enter the room.
Attachment Package
The sample grader along with sample test cases are available here.
Comments