InverseRMQ
TCO14 Round 2C · 2014-03-26 · by cgy4ever
Problem Statement
For example, if P is the permutation (3,1,4,2,5), then:
- The answer to the query (1,2) is max(3,1)=3.
- The answer to the query (2,4) is max(1,4,2)=4.
- The answer to the query (4,5) is max(2,5)=5.
In this problem, we ask you to solve the inverse problem. You are given the
Constraints
- n will be between 1 and 1,000,000,000, inclusive.
- A will contain between 1 and 50 elements, inclusive.
- A, B, and ans will each contain the same number of elements.
- Each element in A will be between 1 and n, inclusive.
- Each element in B will be between 1 and n, inclusive.
- For all i, A[i] will be less than or equal to B[i].
- Each element in ans will be between 1 and n, inclusive.
5
{1,2,4}
{2,4,5}
{3,4,5}
Returns: "Possible"
This is the example from the problem statement. One valid permutation is (3,1,4,2,5). There are also some other valid permutations.
3
{1,2,3}
{1,2,3}
{3,3,3}
Returns: "Impossible"
The only sequence that corresponds to these queries is (3,3,3), but that is not a permutation.
600
{1,101,201,301,401,501}
{100,200,300,400,500,600}
{100,200,300,400,500,600}
Returns: "Possible"
One valid permutation is the permutation (1,2,3,...,600).
1000000000
{1234,1234}
{5678,5678}
{10000,20000}
Returns: "Impossible"
There is no permutation such that two identical queries have different answers.
8
{1,2,3,4,5,6,7,8}
{1,2,3,4,5,6,7,8}
{4,8,2,5,6,3,7,1}
Returns: "Possible"
The only valid permutation is clearly (4,8,2,5,6,3,7,1).
1000000000
{1}
{1000000000}
{19911120}
Returns: "Impossible"
Obviously, for n=1,000,000,000 the maximum of the entire permutation must be 1,000,000,000.
Submissions are judged against all 143 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class InverseRMQ with a public method string possible(int n, vector<int> A, vector<int> B, vector<int> ans) · 143 test cases · 2 s / 256 MB per case