Codeforces Round 930 (Div. 1)
A
Bitwise Operation Wizard
Codeforces
1700
原题
←B →
翻译中…

This is an interactive problem.

翻译中…

There is a secret sequence $p_0, p_1, \ldots, p_{n-1}$, which is a permutation of $\{0,1,\ldots,n-1\}$.

翻译中…

You need to find any two indices $i$ and $j$ such that $p_i \oplus p_j$ is maximized, where $\oplus$ denotes the bitwise XOR operation.

翻译中…

To do this, you can ask queries. Each query has the following form: you pick arbitrary indices $a$, $b$, $c$, and $d$ ($0 \le a,b,c,d \lt n$). Next, the jury calculates $x = (p_a \mid p_b)$ and $y = (p_c \mid p_d)$, where $|$ denotes the bitwise OR operation. Finally, you receive the result of comparison between $x$ and $y$. In other words, you are told if $x \lt y$, $x \gt y$, or $x = y$.

翻译中…

Please find any two indices $i$ and $j$ ($0 \le i,j \lt n$) such that $p_i \oplus p_j$ is maximum among all such pairs, using at most $3n$ queries. If there are multiple pairs of indices satisfying the condition, you may output any one of them.

翻译中…

Input

翻译中…

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

翻译中…

Interaction

翻译中…

The first line of each test case contains one integer $n$ ($2 \le n \le 10^4$). At this moment, the permutation $p_0, p_1, \ldots, p_{n-1}$ is chosen. The interactor in this task is not adaptive. In other words, the sequence $p$ is fixed in every test case and does not change during the interaction.

翻译中…

To ask a query, you need to pick four indices $a$, $b$, $c$, and $d$ ($0 \le a,b,c,d \lt n$) and print the line of the following form:

翻译中…
  • "? a b c d"
翻译中…

After that, you receive:

翻译中…
  • "<" if $(p_a \mid p_b) \lt (p_c \mid p_d)$;
  • "=" if $(p_a \mid p_b) = (p_c \mid p_d)$;
  • ">" if $(p_a \mid p_b) \gt (p_c \mid p_d)$.
翻译中…

You can make at most $3n$ queries of this form.

翻译中…

Next, if your program has found a pair of indices $i$ and $j$ ($0 \le i, j \lt n$) such that $p_i \oplus p_j$ is maximized, print the line of the following form:

翻译中…
  • "! i j"
翻译中…

Note that this line is not considered a query and is not taken into account when counting the number of queries asked.

翻译中…

After this, proceed to the next test case.

翻译中…

If you make more than $3n$ queries during an interaction, your program must terminate immediately, and you will receive the Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

翻译中…

After printing a query or the answer for a test case, do not forget to output the end of line and flush the output. Otherwise, you will get the verdict Idleness Limit Exceeded. To do this, use:

翻译中…
  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see the documentation for other languages.
翻译中…

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

翻译中…

Hacks

翻译中…

To hack, follow the test format below.

翻译中…

The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). The description of the test cases follows.

翻译中…

The first line of each test case contains one integer $n$ ($2 \le n \le 10^4$).

翻译中…

The second line of each test case contains $n$ integers $p_0,p_1,\ldots,p_{n-1}$, which represent a permutation of integers from $0$ to $n - 1$.

翻译中…

The sum of $n$ over all test cases should not exceed $10^4$.

翻译中…

Note

翻译中…

In the first test case, the hidden permutation is $p=[0,3,1,2]$.

翻译中…

For the query "? 0 2 3 1", the jury return "<" because $(p_0 \mid p_2) = (0 \mid 1) =1 \lt (p_3 \mid p_1) = (2 \mid 3) = 3$.

翻译中…

For the query "? 1 1 2 3", the jury return "=" because $(p_1 \mid p_1) = (3\mid 3)= 3 = (p_2 \mid p_3) = (1 \mid 2)=3$.

翻译中…

For the query "? 1 2 0 3", the jury return ">" because $(p_1 \mid p_2) = (3 \mid 1) = 3 \gt (p_0 \mid p_3) = (0\mid 2)=2$.

翻译中…

The answer $i = 3$ and $j = 2$ is valid: $(p_3 \oplus p_2) = (2 \oplus 1) = 3$ is indeed equal to the maximum possible value of $p_i \oplus p_j$. Another valid answer would be $i=0$ and $j=1$. As the number of queries does not exceed $3n=12$, the answer is considered correct.

翻译中…

In the second test case, $n = 2$, so $p$ is either $[0, 1]$ or $[1, 0]$. In any case, $p_0 \oplus p_1 = 1$ is maximum possible.

翻译中…

样例

Input #1
2
4

<

=

>

2
Output #1
? 0 2 3 1

? 1 1 2 3

? 1 2 0 3

! 3 2

! 0 1
InputCopy OutputCopy
题目信息
题号
1936A
时间限制
2 s
内存限制
256 MB
标签
位运算
构造
贪心
交互题
数学

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