[贪心]Mystic Permutation CF1689B

22 篇文章 0 订阅

Monocarp is a little boy who lives in Byteland and he loves programming.

Recently, he found a permutation of length nn. He has to come up with a mystic permutation. It has to be a new permutation such that it differs from the old one in each position.

More formally, if the old permutation is p1,p2,…,pnp1,p2,…,pn and the new one is q1,q2,…,qnq1,q2,…,qn it must hold that

p1≠q1,p2≠q2,…,pn≠qn.p1≠q1,p2≠q2,…,pn≠qn.

Monocarp is afraid of lexicographically large permutations. Can you please help him to find the lexicographically minimal mystic permutation?

Input

There are several test cases in the input data. The first line contains a single integer tt (1≤t≤2001≤t≤200) — the number of test cases. This is followed by the test cases description.

The first line of each test case contains a positive integer nn (1≤n≤10001≤n≤1000) — the length of the permutation.

The second line of each test case contains nn distinct positive integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n). It's guaranteed that pp is a permutation, i. e. pi≠pjpi≠pj for all i≠ji≠j.

It is guaranteed that the sum of nn does not exceed 10001000 over all test cases.

Output

For each test case, output nn positive integers — the lexicographically minimal mystic permutations. If such a permutation does not exist, output −1−1 instead.

Example

input

Copy

4
3
1 2 3
5
2 3 4 5 1
4
2 3 1 4
1
1

output

Copy

2 3 1
1 2 3 4 5
1 2 4 3
-1

Note

In the first test case possible permutations that are mystic are [2,3,1][2,3,1] and [3,1,2][3,1,2]. Lexicographically smaller of the two is [2,3,1][2,3,1].

In the second test case, [1,2,3,4,5][1,2,3,4,5] is the lexicographically minimal permutation and it is also mystic.

In third test case possible mystic permutations are [1,2,4,3][1,2,4,3], [1,4,2,3][1,4,2,3], [1,4,3,2][1,4,3,2], [3,1,4,2][3,1,4,2], [3,2,4,1][3,2,4,1], [3,4,2,1][3,4,2,1], [4,1,2,3][4,1,2,3], [4,1,3,2][4,1,3,2] and [4,3,2,1][4,3,2,1]. The smallest one is [1,2,4,3][1,2,4,3].

题意: 给你一个全排列,求另一个字典序最小的全排列,要求每个位置的值与之前的全排列不同。

分析: 贪心地想,肯定是1 2 3 4 ... n-1 n这样的全排列最优,但是这样肯定会和原来的全排列有位置重复,对于每一个重复的位置,必须要和另一个位置交换数值,显然不能向前交换,因为这样会让字典序变大,可以向后交换,后面的值一定是递增出现的,所以最优情况下就是和后面1个位置交换,交换以后后面那个位置和前面那个位置上的值一定和原排列不同,对于这个位置而言这种方式才能获得最小的值,由于是和后面一个位置交换,所以当重复位置为末尾位置时,只能和它前面那个位置进行交换了。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;

int a[1005], b[1005];

signed main()
{
	int T;
	cin >> T;
	while(T--){
		int n;
		scanf("%d", &n);
		for(int i = 1; i <= n; i++){
			scanf("%d", &a[i]);
			b[i] = i;
		}
		if(n == 1){
			puts("-1");
			continue;
		}
		for(int i = 1; i < n; i++)
			if(a[i] == b[i])
				swap(b[i], b[i+1]);
		if(a[n] == b[n]) swap(b[n], b[n-1]);
		for(int i = 1; i <= n; i++)
			printf("%d ", b[i]);
		puts("");
	}
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值