California Jones and the Gate to Freedom
Time limit1sMemory limit128 MB
Given n stones and a binary index b, decide whether the chosen n/2 stones are exactly the combination at lexicographic rank b among all size-n/2 subsets.
- Level
Medium6 of 10
- Topics
- Combinatorics, Math, Implementation, Sorting
- Solved
- No attempts yet
Problem
California Jones (the sister of the famous Indiana Jones) is trapped in front of a huge gate and needs your help.
There are stones lying in a row, each marked with a distinct integer. In front of the gate there are exactly holes, and Jones must place stones into them. Which hole a stone goes into does not matter; only which stones are chosen matters.
The gate identifies one choice with a binary number. A binary number names a choice as follows:
- Look at the stones by their position in the input: the 1st, 2nd, ..., -th stone.
- Describe any choice of stones by the ascending list of the positions it uses.
- Sort all possible choices in increasing (lexicographic) order of these position lists. The choice using positions comes first.
- Number the sorted choices .
A binary string encodes a non-negative integer, and that integer is the index of a choice in this ordering.
Given a binary string and a set of stones, decide whether that set is exactly the choice indexed by . If is not a valid index (that is, ), it cannot match any set, so the answer is FALSE.
Input
The input contains several test cases. Each test case begins with the number of stones . The input is terminated by a line with .
For every other test case, is even and . The next integers are the stone identifiers. The test case then gives , the number of queries. Each of the following queries consists of a binary string followed by distinct integers naming the chosen stones. Every named stone is one of the stones, and the length of is at most .
Output
For each query, print a single line containing TRUE if the chosen stones are exactly the choice identified by , and FALSE otherwise.