CF BUDDY
← Problems·

1856D · More Wrong

2100 · divide and conquer, interactive

Problem: This is an interactive problem.

The jury has hidden a permutation^\dagger pp of length nn.

In one query, you can pick two integers ll and rr (1l<rn1 \le l < r \le n) by paying (rl)2(r - l)^2 coins. In return, you will be given the number of inversions^\ddagger in the subarray [pl,pl+1,pr][p_l, p_{l + 1}, \ldots p_r].

Find the index of the maximum element in pp by spending at most 5n25 \cdot n^2 coins.

Note: the grader is not adaptive: the permutation is fixed before any queries are made.

^\dagger A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

^\ddagger The number of inversions in an array is the number of pairs of indices (i,j)(i,j) such that i<ji < j and ai>aja_i > a_j. For example, the array [10,2,6,3][10,2,6,3] contains 44 inversions. The inversions are (1,2),(1,3),(1,4)(1,2),(1,3),(1,4), and (3,4)(3,4).

Input Format: Each test contains multiple test cases. The first line of input contains a single integer tt (1t1001 \le t \le 100) — the number of test cases.

The only line of each test case contains a single integer nn (2n20002 \le n \le 2000) — the length of the hidden permutation pp.

It is guaranteed that the sum of nn over all test cases does not exceed 20002000.

Note: In the first test, the interaction proceeds as follows:

SolutionJuryExplanation2There are 22 test cases.4In the first test case, the hidden permutation is [1,3,2,4][1,3,2,4], with length 44.? 1 3 1The solution requests the number of inversions in the subarray [1,3,2][1,3,2] by paying 44 coins, and the jury responds with 11.? 3 4 0The solution requests the number of inversions in the subarray [2,4][2,4] by paying 11 coin, and the jury responds with 00.! 4 The solution has somehow determined that p4=4p_4 = 4, and outputs it. Since the output is correct, the jury continues to the next test case.2In the second test case, the hidden permutation is [2,1][2,1], with length 22.? 1 2 1The solution requests the number of inversions in the subarray [2,1][2,1] by paying 11 coin, and the jury responds with 11.! 1 The solution has somehow determined that p1=2p_1 = 2, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit.

Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction.

Sample Cases

Case 1

Input

2
4

1

0

2

1

Output

? 1 3

? 3 4

! 4

? 1 2

! 1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback