Find the Permutation

Time limit: 0.1s Memory limit: 64MB Input: Output:

There is a hidden permutation1^{1} P=[P1,P2,,PN]P = [P_1, P_2, \ldots, P_N] of the numbers 1,2,,N1, 2, \ldots, N.

You can ask questions in the form (i,j)(i, j) with 1i,jN1 \leq i, j \leq N. Note that ii and jj does not have to be distinct.

The evaluator responds with the most significant bit of the bitwise AND2^{2} of PiP_i and PjP_j. Here, the most significant bit of a number xx is the exponent of the highest power of 22 that appears when writing xx in base 22. For example, the most significant bit of 5=101(2)5 = 101_{(2)} is 22, since 222^2 appears in the binary representation of 55. If the bitwise AND of PiP_i and PjP_j is 00, the evaluator responds with 1-1.

Task

Your task is to determine the hidden permutation of at most 1 0001\ 000 numbers by asking no more than 30 00030 \ 000 questions.

Interaction protocol

Firstly, you need to include the definitions of the functions which you will use/implement in your program; use #include "find-the-permutation.h".

Please note, you don't need to write any main function. You instead need to implement the following function:

vector<int> solve(int N);

This function will be called a single time by the jury throught the execution of the program.
This function will return the hidden permutation, indexed from 1 (the vector starts with index 00 which is meaningless; the permutation is contained between indexes 11 and NN).

You can ask questions by using this function:

int ask(int i, int j);

where ii and jj describe the question (i,j)(i, j).

For each question, if ii and jj are between 11 and NN (inclusive) and the solution has not asked more than 30 00030 \ 000 questions, you receive the answer.

Otherwise, you receive 100-100 and the evaluator terminates the interaction immediately with an "Wrong answer" verdict.

Once found, you have to provide the hidden permutation to the jury by returning it in the previously described manner.

Constraints and clarifications

  • 1N1 0001 \le N \le 1 \ 000
  • The evaluator is not adaptive, meaning that in each test, the hidden permutation is fixed before your program asks any questions.
  • Let QQ denote the number of questions asked by your program in a test case. Then, your score for this test case is calculated in the following way:
    • If Q>30 000Q > 30 \ 000 you will get 0%0\% of the points for the test;
    • If 25 000<Q30 00025 \ 000 < Q \leq 30 \ 000 you will get 20%20\% of the points for the test;
    • If 20 000<Q25 00020 \ 000 < Q \leq 25 \ 000 you will get 40%40\% of the points for the test;
    • If 15 000<Q20 00015 \ 000 < Q \leq 20 \ 000 you will get 60%60\% of the points for the test;
    • If Q15 000Q \leq 15 \ 000 you will get 100%100\% of the points for the test.
  • 1^{1}A permutation of the numbers 1,2,,N1, 2, \ldots, N is a sequence of length NN consisting of the numbers 1,2,,N1, 2, \ldots, N, containing each number exactly once.
  • 2^{2}The bitwise AND of two numbers xx and yy is the number that contains the common bits of xx and yy. For example, if x=11=1011(2)x=11 = 1011_{(2)} and y=13=1101(2)y=13 = 1101_{(2)}, then their bitwise AND will be 9=1001(2)9 = 1001_{(2)}.

Example

vector<int> solve(int N) // suppose N = 5
{
   ask(1, 5); // 2
   ask(2, 3); // 1
   ask(4, 3); // -1
   ask(4, 4); // 0
   return vector<int>{-100, 4, 3, 2, 1, 5};
}

Explanation

Suppose that the hidden permutation is P=[4,3,2,1,5]P = [4,3,2,1,5].

The first question asks the most significant bit of the numbers P1=4P_1 = 4 and P5=5P_5 = 5, which is 22.

The second one asks about the hidden numbers 33 and 22, for which the answer is 11.

The bitwise AND of numbers 22 and 11 is 00 and thus, the answer for the third question will be 1-1.

Lastly, the answer to the fourth question is 00, because the bitwise AND of 11 and 11 is 11.

Note that this is just a sample interaction to showcase the process of asking questions, receiving answers, and guessing the hidden permutation. It does not necessarily represent an approach to finding the hidden permutation.

Log in or sign up to be able to send submissions!