#abc223d. D - Restricted Permutation
D - Restricted Permutation
Score : points
问题描述
在满足以下条件的序列 中,找出字典序最小的序列。这些序列是对 的排列。
- 对于每个 ,在序列 中, 出现的位置早于 。
如果不存在这样的序列 ,则输出 -1
。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
Among the sequences that are permutations of and satisfy the condition below, find the lexicographically smallest sequence.
- For each , appears earlier than in .
If there is no such , print -1
.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
4 3
2 1
3 4
2 4
Sample Output 1
2 1 3 4
The following five permutations satisfy the condition: $(2, 1, 3, 4), (2, 3, 1, 4), (2, 3, 4, 1), (3, 2, 1, 4), (3, 2, 4, 1)$. The lexicographically smallest among them is .
Sample Input 2
2 3
1 2
1 2
2 1
Sample Output 2
-1
No permutations satisfy the condition.
update @ 2024/3/10 09:49:28