Codeforces Round 1073 (Div. 2)
E
Comparable Permutations
Codeforces
未评级
原题
← D2F →
翻译中…

This is an interactive problem.

翻译中…

For a permutation$^{\text{∗}}$ $g = (g_1, g_2, \ldots, g_m)$, define its reverse as $\operatorname{rev}(g) = (g_m, g_{m-1}, \ldots, g_1)$.

翻译中…

There is a hidden permutation $p$ of size $n$. Your task is to find the lexicographically smallest$^{\text{†}}$ permutation $q$ of size $n$ such that $q \gt p$ and $\operatorname{rev}(q) \gt \operatorname{rev}(p)$.

翻译中…

However, you do not need to output $q$ itself. Instead, you must output a permutation $r$ of size $n$ such that $q_i = p_{r_i}$ for all $1 \le i \le n$. If no such $q$ exists, report that instead.

翻译中…

To find the answer, you can ask at most $3n$ queries of the form $\texttt{? } i \; j$ ($1 \le i, j \le n$). The interactor responds with $1$ if $p_i \lt p_j$ and $0$ otherwise. The interactor is not adaptive, which means that the permutation $p$ is fixed throughout the interaction.

翻译中…

$^{\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). $^{\text{†}}$ An array $x$ is lexicographically smaller than array $y$ (so $x \lt y$) if and only if one of the following holds: $x$ is a prefix of $y$, but $x \ne y$; or Let $i$ be the first position where $x_i \neq y_i$ (if it exists), then $x_i \lt y_i$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^4$) — the size of the hidden permutation $p$.

翻译中…

The hidden permutation $p$ is fixed for the test case and does not change during the interaction. In other words, the interactor is not adaptive.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^4$.

翻译中…

Interaction

翻译中…

For each test case, first read a single integer $n$.

翻译中…

You may ask up to $3n$ queries in each test case.

翻译中…

To ask a query, print a line in the format $\texttt{?} \; i \; j$ ($1 \le i, j \le n$).

翻译中…

As a response to the query, you will get:

翻译中…
  • $1$ if $p_i \lt p_j$;
  • $0$ if $p_i \ge p_j$;
  • $-1$ if you made an invalid query or if you exceed the limit of $3n$ queries.
翻译中…

If your program makes more than $3n$ queries for one test case, or makes an invalid query, then the response to the query will be "-1". After receiving such a response, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, it may receive any other verdict.

翻译中…

When you determine the answer for the current test case, print one of the following:

翻译中…
  • If no valid permutation $q$ exists, print $\texttt{!} \; -1$.
  • Otherwise, print $\texttt{!} \; r_1 \; r_2 \; \ldots \; r_n$, where $r$ is a permutation of size $n$ and $q_i = p_{r_i}$.
翻译中…

Note that this output does not count towards the query limit of $3n$.

翻译中…

After that, proceed to the next test case or terminate your program if there are no more test cases.

翻译中…

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.

翻译中…

Note that the interactor is not adaptive, which means that the hidden permutation is unchanged throughout the interaction.

翻译中…

Hacks

翻译中…

To hack, use the following format.

翻译中…

The first line should contain a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.

翻译中…

The first line of each test case should contain a positive integer $n$ ($2 \le n \le 2 \cdot 10^4$) — the size of $p$.

翻译中…

The second line of each test case should contain $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$).

翻译中…

You have to guarantee that given $p$ is a permutation and that the sum of $n$ over all test cases does not exceed $2 \cdot 10^4$.

翻译中…

$^{\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 example, the interaction proceeds as follows.

翻译中…

SolutionJuryExplanation3There are $3$ test cases.5In the first test case, the hidden permutation is $[5, 4, 2, 3, 1]$, with length $5$. The corresponding permutation $q = [5, 4, 3, 1, 2]$? 2 11The solution asks whether $p_2 \lt p_1$, and the jury responds with $1$, as $4 \lt 5$.! 1 2 4 5 3The solution has miraculously determined the answer and prints $r = [1, 2, 4, 5, 3]$, because, for instance, $q_3 = p_{r_3} = p_4 = 3$ and $q_2 = p_{r_2} = p_2 = 4$.4In the second test case, the hidden permutation is $[3, 1, 2, 4]$, with length $4$. The permutation $q$ does not exist for that case.? 2 31The solution asks whether $p_2 \lt p_3$, and the jury responds with $1$.? 1 30The solution asks whether $p_1 \lt p_3$, and the jury responds with $0$.? 1 41The solution asks whether $p_1 \lt p_4$, and the jury responds with $1$.? 2 20The solution asks whether $p_2 \lt p_2$, and the jury responds with $0$.! -1The solution now knows that $p = [3, 1, 2, 4]$ and determines that the corresponding $q$ does not exist, so it prints $-1$.9In the third test case, $p = [8, 1, 9, 3, 5, 4, 2, 6, 7]$, with length $n = 9$.! 1 2 3 6 7 4 5 8 9The solution decides to guess the answer and outputs $r = [1, 2, 3, 6, 7, 4, 5, 8, 9]$, which turns out to be correct.

翻译中…

Note that the empty lines in the example input and output are for the sake of clarity and do not occur in the real interaction.

翻译中…

样例

Input #1
3
5

1

4

1

0

1

0

9
Output #1
? 2 1

! 1 2 4 5 3

? 2 3

? 1 3

? 1 4

? 2 2

! -1

! 1 2 3 6 7 4 5 8 9
InputCopy OutputCopy
题目信息
题号
2191E
时间限制
2 s
内存限制
256 MB

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