A15616 | Amazing Trick
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Alice is a magician and she creates a new trick. She has $n$ cards with different numbers from $1$ to $n$ written on them. First, she asks an audience member to shuffle the deck and put cards in a row. Let's say the $i$ -th card from the left has the number $a_i$ on it.
Then Alice picks two permutations $p$ and $q$ . There is a restriction on $p$ and $q$ — permutations can't have fixed points. Which means $\forall i: p_i \ne i\ and\ q_i \ne i$ .
After permutations are chosen, Alice shuffles the cards according to them. Now the $i$ -th card from the left is the card $a[p[q[i]]$ . The trick is considered successful if $i$ -th card from the left has the number $i$ on it after the shuffles.
Help Alice pick the permutations $p$ and $q$ or say it is not possible for the specific starting permutation $a$ .
Then Alice picks two permutations $p$ and $q$ . There is a restriction on $p$ and $q$ — permutations can't have fixed points. Which means $\forall i: p_i \ne i\ and\ q_i \ne i$ .
After permutations are chosen, Alice shuffles the cards according to them. Now the $i$ -th card from the left is the card $a[p[q[i]]$ . The trick is considered successful if $i$ -th card from the left has the number $i$ on it after the shuffles.
Help Alice pick the permutations $p$ and $q$ or say it is not possible for the specific starting permutation $a$ .
输入格式
The first line of the input contains the number of tests $t$ ( $1 \leq t \leq 10^5$ ).
Each test is described in two lines. The first line contains one integer $n$ — the number of cards ( $1 \leq n \leq 10^5$ ). The second line contains $n$ integers $a_i$ — the initial permutation of the cards ( $1 \leq a_i \leq n$ ; $\forall i \neq j: a_i \neq a_j$ ).
It is guaranteed that the sum of $n$ over all tests does not exceed $10^5$ .
Each test is described in two lines. The first line contains one integer $n$ — the number of cards ( $1 \leq n \leq 10^5$ ). The second line contains $n$ integers $a_i$ — the initial permutation of the cards ( $1 \leq a_i \leq n$ ; $\forall i \neq j: a_i \neq a_j$ ).
It is guaranteed that the sum of $n$ over all tests does not exceed $10^5$ .
输出格式
Print the answer for each test case in the same order the cases appear in the input.
For each test case, print "Impossible" in a single line, if no solution exists.
Otherwise, print "Possible" in the first line, and in the following two lines print permutations $p$ and $q$ .
For each test case, print "Impossible" in a single line, if no solution exists.
Otherwise, print "Possible" in the first line, and in the following two lines print permutations $p$ and $q$ .
输入输出样例
输入 #1
4 2 2 1 3 1 2 3 4 2 1 4 3 5 5 1 4 2 3
输出 #1
Impossible Possible 3 1 2 2 3 1 Possible 3 4 2 1 3 4 2 1 Possible 4 1 2 5 3 3 1 4 5 2
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted