Codeforces Round 1068 (Div. 2)
E
Shiro's Mirror Duel
Codeforces
2200
原题
← DF →
翻译中…

There's no such thing as luck in this world. The victor is decided before the game even starts.— No Game No Life

翻译中…

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:

翻译中…
  • With probability $0.5$, Sora swaps $p_x$ and $p_y$;
  • With probability $0.5$, Sora swaps $p_{n-x+1}$ and $p_{n-y+1}$.
翻译中…

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).

翻译中…

Input

翻译中…

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.

翻译中…

Interaction

翻译中…

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:

翻译中…
  • $\mathtt{?}\; x\; y$ ($1 \le x \ne y \le n$) — the two indices that Shiro selects in this move.
翻译中…

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: fflush(stdout) or cout.flush() in C++; sys.stdout.flush() in Python; see the documentation for other languages.

翻译中…

Note

翻译中…

In the first test case, $n=5$ and the initial permutation is $[5,1,3,4,2]$.

翻译中…
  • We print ? 1 5. The judge replies 1 5, meaning positions $1$ and $5$ are swapped (no mirroring). The array becomes $[2,1,3,4,5]$.
  • We print ? 4 5. The judge replies 2 1, which is the mirror of ${4,5}$ because with $n=5$ we have $n-4+1=2$ and $n-5+1=1$. Thus we must swap positions $2$ and $1$. The array becomes $[1,2,3,4,5]$.
  • The permutation is now sorted, so we print !. The answer line does not count toward the operation limit.
翻译中…

In the second test case, the given permutation is already increasing, so we just need to output !.

翻译中…

样例

Input #1
2
5
5 1 3 4 2

1 5

2 1

2
1 2
Output #1
? 1 5

? 4 5

!

!
InputCopy OutputCopy
题目信息
题号
2173E
时间限制
3 s
内存限制
256 MB
标签
构造
贪心
交互题
概率论
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。