Codeforces Round 761 (Div. 2)
D2
Too Many Impostors (hard version)
Codeforces
2400
原题
← D1E →
翻译中…

This is an interactive problem. The only difference between the easy and hard version is the limit on number of questions.

翻译中…

There are $n$ players labelled from $1$ to $n$. It is guaranteed that $n$ is a multiple of $3$.

翻译中…

Among them, there are $k$ impostors and $n-k$ crewmates. The number of impostors, $k$, is not given to you. It is guaranteed that $\frac{n}{3} \lt k \lt \frac{2n}{3}$.

翻译中…

In each question, you can choose three distinct integers $a$, $b$, $c$ ($1 \le a, b, c \le n$) and ask: "Among the players labelled $a$, $b$ and $c$, are there more impostors or more crewmates?" You will be given the integer $0$ if there are more impostors than crewmates, and $1$ otherwise.

翻译中…

Find the number of impostors $k$ and the indices of players that are impostors after asking at most $n+6$ questions.

翻译中…

The jury is adaptive, which means the indices of impostors may not be fixed beforehand and can depend on your questions. It is guaranteed that there is at least one set of impostors which fulfills the constraints and the answers to your questions at any time.

翻译中…

Input

翻译中…

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

翻译中…

The first and only line of each test case contains a single integer $n$ ($6 \le n \lt 10^4$, $n$ is a multiple of $3$) — the number of players.

翻译中…

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

翻译中…

Interaction

翻译中…

For each test case, the interaction starts with reading $n$.

翻译中…

Then you are allowed to make at most $n+6$ questions in the following way:

翻译中…

"? a b c" ($1 \le a, b, c \le n$, $a$, $b$ and $c$ are pairwise distinct).

翻译中…

After each one, you should read an integer $r$, which is equal to $0$ if there are more impostors than crewmates among players labelled $a$, $b$ and $c$, and equal to $1$ otherwise.

翻译中…

Answer $-1$ instead of $0$ or $1$ means that you made an invalid query. Exit immediately after receiving $-1$ and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.

翻译中…

When you have found the indices of all impostors, print a single line "! " (without quotes), followed by the number of impostors $k$, followed by $k$ integers representing the indices of the impostors. Please note that you must print all this information on the same line.

翻译中…

After printing the answer, your program must then continue to solve the remaining test cases, or exit if all test cases have been solved.

翻译中…

After printing the queries and answers do not forget to output end of line and flush the output buffer. Otherwise, you will get the Idleness limit exceeded verdict. To do flush use:

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

Hacks

翻译中…

You cannot make hacks in this problem.

翻译中…

Note

翻译中…

Explanation for example interaction (note that this example only exists to demonstrate the interaction procedure and does not provide any hint for the solution):

翻译中…

For the first test case:

翻译中…

Question "? 1 2 3" returns $0$, so there are more impostors than crewmates among players $1$, $2$ and $3$.

翻译中…

Question "? 3 4 5" returns $1$, so there are more crewmates than impostors among players $3$, $4$ and $5$.

翻译中…

Outputting "! 3 4 1 2" means that one has found all the impostors, by some miracle. There are $k = 3$ impostors. The players who are impostors are players $4$, $1$ and $2$.

翻译中…

For the second test case:

翻译中…

Question "? 7 1 9" returns $1$, so there are more crewmates than impostors among players $7$, $1$ and $9$.

翻译中…

Outputting "! 4 2 3 6 8" means that one has found all the impostors, by some miracle. There are $k = 4$ impostors. The players who are impostors are players $2$, $3$, $6$ and $8$.

翻译中…

样例

Input #1
2
6

0

1

9

1
Output #1
? 1 2 3

? 3 4 5

! 3 4 1 2

? 7 1 9

! 4 2 3 6 8
InputCopy OutputCopy
题目信息
题号
1617D2
时间限制
3 s
内存限制
256 MB
标签
构造
模拟
交互题
数学

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