Shuffle
Time limit1sMemory limit512 MB
Recover the episode number on each CD by grouping CDs into boxes and asking for the set of episodes in each box, using at most Q queries.
- Level
Medium6 of 10
- Topics
- Array, Bit manipulation, Implementation
- Solved
- No attempts yet
Problem
Lim Li loves anime and has recently bought a complete season of Umaru from Amazon. The episodes of Umaru come in a set of CDs, with one episode on each CD. The CDs are labelled , and the episodes are also numbered . She was ready to start watching when she realised that, due to a manufacturing fault, the episode numbers do not match the labels on the CDs. She contacted Amazon, which said the CD labels do not match the episode numbers, but the episodes are only shuffled and no single episode is missing from the set. To avoid spoilers, Lim Li does not want to play the CDs herself to find which episode is on each CD, so she asks her friend Rar the Cat for help. The procedure is as follows. Lim Li groups her CDs into boxes with CDs each (). The boxes are shipped to Rar. In transit, the CDs in each box may move around within the box, and the boxes may arrive in a different order. Rar plays the CDs on his own CD player and writes down the set of episodes he finds in each box. He sends this information back to Lim Li on separate pieces of paper, each listing the episode numbers inside one box. The entire set of CDs is then returned to Lim Li. This procedure can be done a maximum of times, after which Rar gets annoyed and stops helping. Find the episode number on each CD with as little help from Rar as possible.
Constraints
, , and .
Hint
Suppose , , , , and the test case belongs to Subtask 2. The episodes are . Your function is called with solve(6, 3, 2, 100, 2). A possible interaction: shuffle([[1, 2], [3, 4], [5, 6]]) = [[6, 2], [5, 4], [3, 1]]. The boxes are misordered and the CDs in each box are shuffled, so Rar may receive the boxes as [[6, 5], [4, 3], [1, 2]] and reports [[6, 2], [5, 4], [3, 1]]. Next, shuffle([[2, 6], [3, 1], [5, 4]]) = [[6, 1], [2, 5], [4, 3]], where the boxes are shuffled and Rar reports [[6, 1], [2, 5], [4, 3]]. Then shuffle([[6, 5], [4, 2], [3, 1]]) = [[5, 1], [3, 4], [2, 6]]. Your program then decides it has enough information and concludes that the episodes are , so it returns . The episodes are correct and fewer than 100 queries were used, so this test case is accepted.