Ace in the Hole (Small)
Time limit30sMemory limit512 MB
Reconstruct the lexicographically greatest 321-avoiding permutation consistent with the given optimal worst-case search order for value 1.
- Level
Hard8 of 10
- Topics
- Game theory, Brute force, Combinatorics
- Solved
- No attempts yet
Problem
Amy builds a deck of cards, each carrying one value from to . She arranges the deck so that the values contain no decreasing subsequence of length . For example, is not allowed because decreases.
Amy hands the deck to Ben. Ben knows the deck has no decreasing subsequence of length , but he does not know the arrangement. Ben wants to find the card with value . He picks a card, turns it over to read its value, and repeats until he finds value . At every step Ben picks a card that minimizes the worst-case number of cards he still has to examine.
Ben later says he was unlucky and examined all cards before he found value . Given the order in which Ben examined the cards, find the value of each card. If several decks are possible, choose the lexicographically greatest one.
Deck is lexicographically greater than deck when, at the first position where the two differ, the card in has the greater value.
Take with Ben examining positions in the order , where positions are counted from . The values must have been . If card held value , Ben would have stopped at once. If card held value , Ben would have known that card holds value , because the arrangement contains a decreasing subsequence of length and is impossible. Neither case needs a third examination. Card therefore held value . For the same reason card did not hold value . The values are .
Input
The first line contains the number of test cases . Each test case begins with a line containing one integer , the number of cards in the deck. The next line contains integers separated by single spaces describing the order in which Ben examined the deck. The -th integer is the position of the card Ben examined -th, with positions counted from .
Output
For each test case print one line in the form Case #x: y, where is the test case number starting from and is the values of the cards in position order, separated by single spaces.
Constraints
- For the given examination order, at least one deck satisfies every condition in the problem, including the condition that Ben had to examine all cards.