A14850 | Hidden Permutations
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
This is an interactive problem.
The jury has a permutation $p$ of length $n$ and wants you to guess it. For this, the jury created another permutation $q$ of length $n$ . Initially, $q$ is an identity permutation ( $q_i = i$ for all $i$ ).
You can ask queries to get $q_i$ for any $i$ you want. After each query, the jury will change $q$ in the following way:
- At first, the jury will create a new permutation $q'$ of length $n$ such that $q'_i = q_{p_i}$ for all $i$ .
- Then the jury will replace permutation $q$ with pemutation $q'$ .
You can make no more than $2n$ queries in order to quess $p$ .
The jury has a permutation $p$ of length $n$ and wants you to guess it. For this, the jury created another permutation $q$ of length $n$ . Initially, $q$ is an identity permutation ( $q_i = i$ for all $i$ ).
You can ask queries to get $q_i$ for any $i$ you want. After each query, the jury will change $q$ in the following way:
- At first, the jury will create a new permutation $q'$ of length $n$ such that $q'_i = q_{p_i}$ for all $i$ .
- Then the jury will replace permutation $q$ with pemutation $q'$ .
You can make no more than $2n$ queries in order to quess $p$ .
输入格式
The first line of input contains a single integer $t$ ( $1 \leq t \leq 1000$ ) — the number of test cases.
输出格式
Interaction in each test case starts after reading the single integer $n$ ( $1 \leq n \leq 10^4$ ) — the length of permutations $p$ and $q$ .
To get the value of $q_i$ , output the query in the format $?$ $i$ ( $1 \leq i \leq n$ ). After that you will receive the value of $q_i$ .
You can make at most $2n$ queries. After the incorrect query you will receive $0$ and you should exit immediately to get Wrong answer verdict.
When you will be ready to determine $p$ , output $p$ in format $!$ $p_1$ $p_2$ $\ldots$ $p_n$ . After this you should go to the next test case or exit if it was the last test case. Printing the permutation is not counted as one of $2n$ queries.
After printing a query do not forget to output 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 documentation for other languages.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^4$ . The interactor is not adaptive in this problem.
Hacks:
To hack, use the following format:
The first line contains the single integer $t$ — the number of test cases.
The first line of each test case contains the single integer $n$ — the length of the permutations $p$ and $q$ . The second line of each test case contains $n$ integers $p_1, p_2, \ldots, p_n$ — the hidden permutation for this test case.
To get the value of $q_i$ , output the query in the format $?$ $i$ ( $1 \leq i \leq n$ ). After that you will receive the value of $q_i$ .
You can make at most $2n$ queries. After the incorrect query you will receive $0$ and you should exit immediately to get Wrong answer verdict.
When you will be ready to determine $p$ , output $p$ in format $!$ $p_1$ $p_2$ $\ldots$ $p_n$ . After this you should go to the next test case or exit if it was the last test case. Printing the permutation is not counted as one of $2n$ queries.
After printing a query do not forget to output 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 documentation for other languages.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^4$ . The interactor is not adaptive in this problem.
Hacks:
To hack, use the following format:
The first line contains the single integer $t$ — the number of test cases.
The first line of each test case contains the single integer $n$ — the length of the permutations $p$ and $q$ . The second line of each test case contains $n$ integers $p_1, p_2, \ldots, p_n$ — the hidden permutation for this test case.
输入输出样例
输入 #1
2 4 3 2 1 4 2 4 4
输出 #1
? 3 ? 2 ? 4 ! 4 2 1 3 ? 2 ? 3 ? 2 ! 1 3 4 2
In the first test case the hidden permutation $p = [4, 2, 1, 3]$ .
Before the first query $q = [1, 2, 3, 4]$ so answer for the query will be $q_3 = 3$ .
Before the second query $q = [4, 2, 1, 3]$ so answer for the query will be $q_2 = 2$ .
Before the third query $q = [3, 2, 4, 1]$ so answer for the query will be $q_4 = 1$ .
In the second test case the hidden permutation $p = [1, 3, 4, 2]$ .
Empty strings are given only for better readability. There will be no empty lines in the testing system.
Before the first query $q = [1, 2, 3, 4]$ so answer for the query will be $q_3 = 3$ .
Before the second query $q = [4, 2, 1, 3]$ so answer for the query will be $q_2 = 2$ .
Before the third query $q = [3, 2, 4, 1]$ so answer for the query will be $q_4 = 1$ .
In the second test case the hidden permutation $p = [1, 3, 4, 2]$ .
Empty strings are given only for better readability. There will be no empty lines in the testing system.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted