| Codeforces Round 1068 (Div. 2) |
|---|
| Finished |
This is an interactive problem.
One day, Sora and Shiro feel bored again, so they decide to settle it with a game.
At the beginning, Sora gives Shiro a permutation$$$^{\text{∗}}$$$ $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$. In each operation, Shiro may select two distinct indices $$$x$$$ and $$$y$$$ ($$$1\le x\ne y\le n$$$). Then Sora flips a fair coin:
After the operation, Sora replies with the actual pair of indices that were swapped, so that Shiro can update her local permutation accordingly.
Shiro's goal is to sort the permutation $$$p$$$ in ascending order by using at most $$$\lfloor 2.5n+800\rfloor$$$ operations. Help her!
$$$^{\text{∗}}$$$A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1\le n\le 4000$$$) — the length of $$$p$$$.
The second line contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ — the elements of $$$p$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^4$$$.
It is guaranteed that there are $$$50$$$ tests in this problem.
For each test case, you can use at most $$$\lfloor 2.5n+800\rfloor$$$ moves to sort the permutation $$$p$$$ in ascending order.
To make a move, you should print a new line in the following format:
As a response to the move, you will receive two integers $$$u$$$ and $$$v$$$ — the actual pair of indices that Sora swapped. It is guaranteed that $$$(u, v)$$$ is randomly selected from $$$(x, y)$$$ and $$$(n-x+1, n-y+1)$$$ with equal probability.
To report that the permutation has been sorted in ascending order, you should print a new line with a single character $$$\mathtt{!}$$$. This does not count as one of the $$$\lfloor 2.5n+800\rfloor$$$ moves.
After that, proceed to the next test case or terminate the program if it was the last test case.
After printing each query do not forget to output the end of line and flush$$$^{\text{∗}}$$$ the output. Otherwise, you will get Idleness limit exceeded verdict.
If, at any interaction step, you read $$$-1$$$ instead of valid data, your solution must exit immediately. This means that your solution will receive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream.
Hacks are disabled in this problem.
$$$^{\text{∗}}$$$To flush, use:
2 5 5 1 3 4 2 1 5 2 1 2 1 2
? 1 5 ? 4 5 ! !
In the first test case, $$$n=5$$$ and the initial permutation is $$$[5,1,3,4,2]$$$.
In the second test case, the given permutation is already increasing, so we just need to output !.
| Name |
|---|


