CF1656G.Cycle Palindrome

NOI/NOI+/CTSC

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

We say that a sequence of nn integers a1,a2,…,ana_1, a_2, \ldots, a_n is a palindrome if for all 1≤i≤n1 \leq i \leq n, ai=an−i+1a_i = a_{n-i+1}. You are given a sequence of nn integers a1,a2,…,ana_1, a_2, \ldots, a_n and you have to find, if it exists, a cycle permutation σ\sigma so that the sequence aσ(1),aσ(2),…,aσ(n)a_{\sigma(1)}, a_{\sigma(2)}, \ldots, a_{\sigma(n)} is a palindrome.

A permutation of 1,2,…,n1, 2, \ldots, n is a bijective function from 1,2,…,n{1, 2, \ldots, n} to 1,2,…,n{1, 2, \ldots, n}. We say that a permutation σ\sigma is a cycle permutation if 1,σ(1),σ2(1),…,σn−1(1)1, \sigma(1), \sigma^2(1), \ldots, \sigma^{n-1}(1) are pairwise different numbers. Here σm(1)\sigma^m(1) denotes σ(σ(…σ⏟m times(1)…))\underbrace{\sigma(\sigma(\ldots \sigma}_{m \text{ times}}(1) \ldots)).

我们称一个由 nn 个整数组成的序列 a1,a2,…,ana_1, a_2, \ldots, a_n 是回文序列,当且仅当对所有 1≤i≤n1 \leq i \leq n,均有 ai=an−i+1a_i = a_{n-i+1}。现给定一个由 nn 个整数组成的序列 a1,a2,…,ana_1, a_2, \ldots, a_n,你需要判断是否存在一个循环置换 σ\sigma,使得重排后的序列 aσ(1),aσ(2),…,aσ(n)a_{\sigma(1)}, a_{\sigma(2)}, \ldots, a_{\sigma(n)} 构成回文序列。

集合 {1,2,…,n}\{1, 2, \ldots, n\} 的一个置换是指从 {1,2,…,n}\{1, 2, \ldots, n\} 到 {1,2,…,n}\{1, 2, \ldots, n\} 的一个双射函数。我们称一个置换 σ\sigma 是循环置换,当且仅当 1,σ(1),σ2(1),…,σn−1(1)1, \sigma(1), \sigma^2(1), \ldots, \sigma^{n-1}(1) 两两互不相同。此处 σm(1)\sigma^m(1) 表示将 σ\sigma 连续作用 mm 次于 11 所得的结果,即 σ(σ(…σ⏟m 次(1)…))\underbrace{\sigma(\sigma(\ldots \sigma}_{m \text{ 次}}(1) \ldots))。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤3⋅1041 \leq t \leq 3 \cdot 10^4) — the number of test cases. Description of the test cases follows.

The first line of each test case contains an integer nn (2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5) — the size of the sequence.

The second line of each test case contains nn integers a1,…,ana_1, \ldots, a_n (1≤ai≤n1 \leq a_i \leq n).

The sum of nn for all test cases is at most 2⋅1052 \cdot 10^5.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤3⋅1041 \leq t \leq 3 \cdot 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5),表示序列的长度。

每个测试用例的第二行包含 nn 个整数 a1,…,ana_1, \ldots, a_n(1≤ai≤n1 \leq a_i \leq n)。

所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output one line with YES if a cycle permutation exists, otherwise output one line with NO.

If the answer is YES, output one additional line with nn integers σ(1),σ(2),…,σ(n)\sigma(1), \sigma(2), \ldots, \sigma(n), the permutation. If there is more than one permutation, you may print any.

对于每个测试用例,如果存在一个循环排列,则输出一行 YES;否则输出一行 NO。

如果答案为 YES,则再输出一行包含 nn 个整数 σ(1),σ(2),…,σ(n)\sigma(1), \sigma(2), \ldots, \sigma(n) 的排列。若存在多个满足条件的排列,输出任意一个即可。

输入输出样例

  • 输入#1

    3
    4
    1 2 2 1
    3
    1 2 1
    7
    1 3 3 3 1 2 2

    输出#1

    YES
    3 1 4 2 
    NO
    YES
    5 3 7 2 6 4 1

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

首页