Median Queries Solution Codeforces Round #723
SOLUTION
” CLICK HERE “
This is an interactive problem.
There is a secret permutation (-indexed) of numbers from to . More formally, for , and for , . It is known that .
In query, you give distinct integers (), and receive the median of .
In this case, the median is the -nd element (-indexed) of the sequence when sorted in non-decreasing order. The median of is and the median of is .
Can you find the secret permutation in not more than queries?
Note: the grader is not adaptive: the permutation is fixed before any queries are made.
The first line of input contains a single integer — the number of testcases.
The first line of each testcase consists of a single integer — the length of the secret permutation.
It is guaranteed that the sum of over all test cases does not exceed .
For each testcase, you begin the interaction by reading .
To perform a query, output "? a b c" where is the indices you want to use for the query.
Numbers have to satisfy and ,,.
For each query, you will receive a single integer : the median of .
In case your query is invalid or you asked more than queries, the interactor will print "−1" and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you have determined the secret permutation, output "! p[1] p[2] ... p[n]". If the secret permutation is correct, the interactor will print "1". Otherwise, the interactor will print "-1" and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded verdict.
To do this, use:
- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- see documentation for other languages.
Hacks:
To hack, use the following format of test:
The first line should contain a single integer () — the number of testcases.
The first line of each testcase should contain a single integer () — the length of the secret permutation.
The following line of should contain integers . and must be a permutation of integers from to .
You must ensure that the sum of over all testcases does not exceed .
1 20 6 9 1
? 1 5 2 ? 20 19 2 ! 9 10 19 7 16 18 11 14 15 6 20 8 17 4 5 3 12 2 13 1
The secret permutation is .
For the first query, the values of is . Since , and . The return value is the median of which is .
For the second query, the values of is . Since , and . The return value is the median of which is .
By some miracle, we have figured out that the secret permutation is . We output it and receive from the interactor, meaning that we have guessed the secret permutation correctly.