310320: CF1815B. Sum Graph

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

B. Sum Graphtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

This is an interactive problem.

There is a hidden permutation $p_1, p_2, \dots, p_n$.

Consider an undirected graph with $n$ nodes only with no edges. You can make two types of queries:

  1. Specify an integer $x$ satisfying $2 \le x \le 2n$. For all integers $i$ ($1 \le i \le n$) such that $1 \le x-i \le n$, an edge between node $i$ and node $x-i$ will be added.
  2. Query the number of edges in the shortest path between node $p_i$ and node $p_j$. As the answer to this question you will get the number of edges in the shortest path if such a path exists, or $-1$ if there is no such path.

Note that you can make both types of queries in any order.

Within $2n$ queries (including type $1$ and type $2$), guess two possible permutations, at least one of which is $p_1, p_2, \dots, p_n$. You get accepted if at least one of the permutations is correct. You are allowed to guess the same permutation twice.

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 a single integer $t$ ($1 \le t \le 100$) — the number of test cases.

The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$) — the length of the permutation.

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

Interaction

The interaction for each test case begins by reading the integer $n$.

Then, make at most $2n$ queries:

  • If you want to make a type $1$ query, output "+ x". $x$ must be an integer between $2$ and $2n$ inclusive. After doing that read $1$ or $-2$. If you read $1$ your query was valid, otherwise it was invalid or you exceed the limit of queries, and your program must terminate immediately to receive a Wrong Answer verdict.
  • If you want to make a type $2$ query, output "? i j". $i$ and $j$ must be integers between $1$ and $n$ inclusive. After that, read in a single integer $r$ ($-1 \le r \le n$) — the answer to your query. If you receive the integer $−2$ instead of an answer, it means your program has made an invalid query, or has exceeded the limit of queries. Your program must terminate immediately to receive a Wrong Answer verdict.

At any point of the interaction, if you want to guess two permutations, output "! $p_{1,1}$ $p_{1,2}$ $\dots$ $p_{1,n}$ $p_{2,1}$ $p_{2,2}$ $\dots$ $p_{2,n}$". Note that you should output the two permutations on the same line, and no exclamation mark is needed to separate the two permutations. After doing that read $1$ or $-2$. If you read $1$ your answer was correct, otherwise it was incorrect and your program must terminate immediately to receive a Wrong Answer verdict. After that, move on to the next test case, or terminate the program if there are none. Note that reporting the answer does not count as a query.

Note that even if you output a correct permutation, the second permutation should be a permutation and not an arbitrary array.

At any point, if you continue interaction after reading in the integer $-2$, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

After printing a query or the answer do not forget to output the end of line and flush the output. Otherwise, you will get 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.

Interactor is non-adaptive. This means that all permutations are fixed before the interaction starts.

Hacks

To make a hack, use the following format.

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

The first line of each test case should contain a single integer $n$ ($2 \le n \le 10^3$) — the length of the permutation.

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

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

ExampleInput
2
6

1

1

1

1

1

2

-1

1
2

1
Output

+ 12

+ 2

+ 3

? 1 3

+ 5

? 1 5

? 4 5

! 1 4 2 5 3 6 1 2 3 4 5 6


! 1 2 2 1

Note

In the first test case, $n=6$ and the hidden permutation $p = [1,4,2,5,3,6]$.

Firstly, make a type $1$ query on $x=12, 2, 3$ respectively. This adds four edges to the graph in total:

  • An edge that connects node $6$ and node $6$.
  • An edge that connects node $1$ and node $1$.
  • An edge that connects node $1$ and node $2$.
  • An edge that connects node $2$ and node $1$.

Since all of these queries are valid, the interactor returns $1$ after each of them.

Then, query the number of edges in the shortest path between node $p_1 = 1$ and $p_3 = 2$, which is equal to $1$.

Then, make a type $1$ query on $x=5$. This adds four edges to the graph in total:

  • An edge that connects node $1$ and node $4$.
  • An edge that connects node $2$ and node $3$.
  • An edge that connects node $3$ and node $2$.
  • An edge that connects node $4$ and node $1$.

Since this query is valid, the interactor returns $1$.

Then, query the number of edges in the shortest path between node $p_1 = 1$ and $p_5 = 3$, which is equal to $2$.

Then, query the number of edges in the shortest path between node $p_4 = 5$ and $p_5 = 3$. Such a path doesn't exist, therefore the interactor returns $-1$.

Afterwards, due to some magic, two possible permutations that can be $p$ are determined: the first permutation is $[1,4,2,5,3,6]$ and the second permutation is $[1,2,3,4,5,6]$. Since the first permutation is equal to the hidden permutation, this test case is solved correctly. In total, $7$ queries are used, which is within the limit of $2 \cdot 6 = 12$ queries.

Since the answer is correct, the interactor returns $1$.

In the second test case, $n=2$ and the hidden permutation is $p = [2,1]$.

Since there are only $2! = 2$ possible permutations, no queries are needed. It is sufficient to just output the two permutations, $[1,2]$ and $[2,1]$. In total, $0$ queries are used, which is within the limit of $2 \cdot 2 = 4$ queries.

Since the answer is correct, the interactor returns $1$.

Input

题意翻译

你需要通过至多 $2n$ 次询问来确定一个长度为 $n$ 的排列,每次你可以进行两种询问之一 - 选择一个 $x(1<x\leq2n)$ ,对于所有的 $i$ ,只要满足$i$ 和 $x-i$ 都在 $[1,n]$ 的区间中,就连一条 $i$ 到 $x - i $的边 - 选择一组 $i,j$ ,询问 $p_i,p_j$ 在最短路上的距离,如果没有则会返回 -1 你可以按任何顺序进行这两种类型的查询 输出 "+ x" 获得第一类“询问”,输出"? i j"获得第二类询问。 对于第一类询问,如果你的询问是合法的且没有超过询问次数限制则会返回1,否则会返回-2. 对于第一类询问,如果你的询问是合法的且没有超过询问次数限制则会返回相应的结果,否则会返回-2. 回答答案时,你可以输出两个排列,只要有其中一个排列为正确答案即视为正确。你可以输出两个相同的排列。 你需要把两个排列在同一行进行输出,即 $! $&ensp;$ p_1,p_2……p_n,p_1,p_2……p_n$ 数字之间用空格隔开

Output

题目大意:

这是一个交互问题。有一个隐藏的排列 p_1, p_2, \ldots, p_n。考虑一个只有 n 个节点且没有边的无向图。你可以进行两种类型的查询:

1. 指定一个整数 x 满足 2 \le x \le 2n。对于所有整数 i (1 \le i \le n) 使得 1 \le x-i \le n,将添加节点 i 和节点 x-i 之间的边。
2. 查询节点 p_i 和节点 p_j 之间最短路径中的边数。如果存在这样的路径,你将得到最短路径中的边数,否则得到 -1。

注意,你可以以任意顺序进行这两种类型的查询。

在 2n 次查询(包括类型 1 和类型 2)内,猜测两个可能的排列,其中至少一个是 p_1, p_2, \ldots, p_n。如果你猜对至少一个排列,你将得到接受。你可以猜测同一个排列两次。

输入输出数据格式:

输入:

每个测试包含多个测试用例。第一行包含一个整数 t (1 \le t \le 100) —— 测试用例的数量。

每个测试用例的第一行包含一个整数 n (2 \le n \le 10^3) —— 排列的长度。

保证所有测试用例的 n 之和不超过 10^3。

输出:

对于每个测试用例,首先读取整数 n。

然后进行最多 2n 次查询:

- 如果你想进行类型 1 的查询,输出 "+ x"。x 必须是一个介于 2 和 2n(包括 2 和 2n)之间的整数。之后读取 1 或 -2。如果你读到 1,你的查询是有效的,否则它是无效的,或者你超过了查询限制,你的程序必须立即终止以获得错误答案。
- 如果你想进行类型 2 的查询,输出 "? i j"。i 和 j 必须是介于 1 和 n(包括 1 和 n)之间的整数。之后,读取一个整数 r (-1 \le r \le n) —— 你的查询的答案。如果你收到整数 -2 而不是答案,这意味着你的程序进行了无效查询,或者超过了查询限制。你的程序必须立即终止以获得错误答案。

在任何时候,如果你在读取整数 -2 之后继续交互,你可能会得到任意判决,因为你的解决方案将继续从已关闭的流中读取。

在打印查询或答案后,不要忘记输出行尾并刷新输出。否则,你将得到超时限制。在 C++ 中使用 fflush(stdout) 或 cout.flush();在 Java 中使用 System.out.flush();在 Pascal 中使用 flush(output);在 Python 中使用 stdout.flush();在其他语言中请查阅文档。

交互器是非自适应的。这意味着所有排列在交互开始前都是固定的。

破解:

要破解,请使用以下格式。

第一行应包含一个整数 t (1 \le t \le 100) —— 测试用例的数量。

每个测试用例的第一行应包含一个整数 n (2 \le n \le 10^3) —— 排列的长度。

每个测试用例的第二行应包含 n 个不同的整数 p_1, p_2, \ldots, p_n (1 \le p_i \le n) —— 隐藏的排列。

所有测试用例的 n 之和不应超过 10^3。题目大意: 这是一个交互问题。有一个隐藏的排列 p_1, p_2, \ldots, p_n。考虑一个只有 n 个节点且没有边的无向图。你可以进行两种类型的查询: 1. 指定一个整数 x 满足 2 \le x \le 2n。对于所有整数 i (1 \le i \le n) 使得 1 \le x-i \le n,将添加节点 i 和节点 x-i 之间的边。 2. 查询节点 p_i 和节点 p_j 之间最短路径中的边数。如果存在这样的路径,你将得到最短路径中的边数,否则得到 -1。 注意,你可以以任意顺序进行这两种类型的查询。 在 2n 次查询(包括类型 1 和类型 2)内,猜测两个可能的排列,其中至少一个是 p_1, p_2, \ldots, p_n。如果你猜对至少一个排列,你将得到接受。你可以猜测同一个排列两次。 输入输出数据格式: 输入: 每个测试包含多个测试用例。第一行包含一个整数 t (1 \le t \le 100) —— 测试用例的数量。 每个测试用例的第一行包含一个整数 n (2 \le n \le 10^3) —— 排列的长度。 保证所有测试用例的 n 之和不超过 10^3。 输出: 对于每个测试用例,首先读取整数 n。 然后进行最多 2n 次查询: - 如果你想进行类型 1 的查询,输出 "+ x"。x 必须是一个介于 2 和 2n(包括 2 和 2n)之间的整数。之后读取 1 或 -2。如果你读到 1,你的查询是有效的,否则它是无效的,或者你超过了查询限制,你的程序必须立即终止以获得错误答案。 - 如果你想进行类型 2 的查询,输出 "? i j"。i 和 j 必须是介于 1 和 n(包括 1 和 n)之间的整数。之后,读取一个整数 r (-1 \le r \le n) —— 你的查询的答案。如果你收到整数 -2 而不是答案,这意味着你的程序进行了无效查询,或者超过了查询限制。你的程序必须立即终止以获得错误答案。 在任何时候,如果你在读取整数 -2 之后继续交互,你可能会得到任意判决,因为你的解决方案将继续从已关闭的流中读取。 在打印查询或答案后,不要忘记输出行尾并刷新输出。否则,你将得到超时限制。在 C++ 中使用 fflush(stdout) 或 cout.flush();在 Java 中使用 System.out.flush();在 Pascal 中使用 flush(output);在 Python 中使用 stdout.flush();在其他语言中请查阅文档。 交互器是非自适应的。这意味着所有排列在交互开始前都是固定的。 破解: 要破解,请使用以下格式。 第一行应包含一个整数 t (1 \le t \le 100) —— 测试用例的数量。 每个测试用例的第一行应包含一个整数 n (2 \le n \le 10^3) —— 排列的长度。 每个测试用例的第二行应包含 n 个不同的整数 p_1, p_2, \ldots, p_n (1 \le p_i \le n) —— 隐藏的排列。 所有测试用例的 n 之和不应超过 10^3。

加入题单

上一题 下一题 算法标签: