CF1773A.Amazing Trick

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Alice is a magician and she creates a new trick. She has nn cards with different numbers from 11 to nn written on them. First, she asks an audience member to shuffle the deck and put cards in a row. Let's say the ii-th card from the left has the number aia_i on it.

Then Alice picks two permutations pp and qq. There is a restriction on pp and qq — permutations can't have fixed points. Which means ∀i:pi≠i and qi≠i\forall i: p_i \ne i\ and\ q_i \ne i.

After permutations are chosen, Alice shuffles the cards according to them. Now the ii-th card from the left is the card a[p[q[i]]a[p[q[i]]. The trick is considered successful if ii-th card from the left has the number ii on it after the shuffles.

Help Alice pick the permutations pp and qq or say it is not possible for the specific starting permutation aa.

Alice 是一位魔术师,她设计了一个新魔术。她有 nn 张卡片,每张卡片上分别写有一个从 11 到 nn 的不同数字。首先,她请一位观众将这副牌洗匀,并将卡片排成一行。设从左往右数第 ii 张卡片上的数字为 aia_i。

接着,Alice 选择两个排列 pp 和 qq。对 pp 和 qq 有一个限制条件:这两个排列都不能有不动点,即对所有 ii,均需满足 pi≠ip_i \ne i 且 qi≠iq_i \ne i。

选定排列后,Alice 按照它们对卡片进行洗牌。洗牌后,从左往右数第 ii 张卡片,是原先排列中编号为 a[p[q[i]]a[p[q[i]] 的卡片(即:先应用 qq,再应用 pp,最后取 aa 中对应位置的值)。若洗牌后从左往右数第 ii 张卡片上的数字恰好为 ii,则该魔术视为成功。

请帮助 Alice 找出满足条件的排列 pp 和 qq;若对给定的初始排列 aa 不存在这样的排列,请说明其不可能性。

输入格式

The first line of the input contains the number of tests tt (1≤t≤1051 \leq t \leq 10^5).

Each test is described in two lines. The first line contains one integer nn — the number of cards (1≤n≤1051 \leq n \leq 10^5). The second line contains nn integers aia_i — the initial permutation of the cards (1≤ai≤n1 \leq a_i \leq n; ∀i≠j:ai≠aj\forall i \neq j: a_i \neq a_j).

It is guaranteed that the sum of nn over all tests does not exceed 10510^5.

输入的第一行包含测试用例的数量 tt(1≤t≤1051 \leq t \leq 10^5)。

每个测试用例由两行描述。第一行包含一个整数 nn —— 卡片的数量(1≤n≤1051 \leq n \leq 10^5)。第二行包含 nn 个整数 aia_i —— 卡片的初始排列(1≤ai≤n1 \leq a_i \leq n;∀i≠j:ai≠aj\forall i \neq j: a_i \neq a_j)。

保证所有测试用例的 nn 之和不超过 10510^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 pp and qq.

按输入中测试用例出现的相同顺序,为每个测试用例输出答案。

对于每个测试用例,若不存在解,则在单独一行中输出 "Impossible"。

否则,在第一行输出 "Possible",并在接下来的两行中分别输出排列 pp 和 qq。

输入输出样例

  • 输入#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

输入解题思路,AI测评打分。不知道怎么写?

首页