#arc178a. A - Good Permutation 2
A - Good Permutation 2
Score : points
问题陈述
给定一个正整数 和一个包含 个正整数的序列 。
这里, 中的所有元素都是 到 (包括两端)之间的不同整数。
如果一个排列 满足以下条件,对于所有整数 满足 :
- 的任意连续子序列都不是 的排列。
则称这个排列为 好排列。
确定是否存在一个 好排列,如果存在,找到字典序最小的 好排列。
什么是字典序?
如果满足以下条件之一,则称序列 字典序小于序列 。这里, 和 分别表示 和 的长度。
- 且 $(S_1, S_2, \ldots, S_{|S|}) = (T_1, T_2, \ldots, T_{|S|})$。
- 存在一个整数 满足以下两个条件:
- $(S_1, S_2, \ldots, S_{i-1}) = (T_1, T_2, \ldots, T_{i-1})$。
- 数字上小于 。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
You are given a positive integer and a sequence of positive integers .
Here, all elements of are distinct integers between and , inclusive.
A permutation of is called a good permutation when it satisfies the following condition for all integers such that :
- No contiguous subsequence of is a permutation of .
Determine whether a good permutation exists, and if it does, find the lexicographically smallest good permutation.
What is lexicographical order?
A sequence is said to be lexicographically smaller than a sequence if one of the following conditions holds. Here, and denote the lengths of and , respectively.
- and $(S_1, S_2, \ldots, S_{|S|}) = (T_1, T_2, \ldots, T_{|S|})$.
- There exists an integer such that both of the following hold:
- $(S_1, S_2, \ldots, S_{i-1}) = (T_1, T_2, \ldots, T_{i-1})$.
- is smaller than (as a number).
Constraints
- All elements of are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
If a good permutation does not exist, print -1
.
If it exists, print the lexicographically smallest good permutation, separated by spaces.
Sample Input 1
4 1
2
Sample Output 1
1 3 2 4
For example, is not a good permutation because it contains as a contiguous subsequence.
Other non-good permutations are and .
Some good permutations are and . Among these, the lexicographically smallest one is , so print it separated by spaces.
Sample Input 2
5 3
4 3 2
Sample Output 2
1 3 4 5 2
Examples of good permutations include , , and .
Examples of non-good permutations include , , and .
Sample Input 3
92 4
16 7 1 67
Sample Output 3
-1
If a good permutation does not exist, print -1
.
Sample Input 4
43 2
43 2
Sample Output 4
-1