Score : 300 points
问题描述
你已知一个排列 P=(P1,…,PN),它是 (1,…,N) 的一个排列,并且满足 (P1,…,PN)=(1,…,N)。
假设排列 P 是所有 (1…,N) 的排列中第 K 小的字典序排列。请找出第 (K−1) 小的字典序排列。
什么是排列?
排列是指将集合 (1,…,N) 中的元素重新排列成的一个序列。
什么是字典序?
对于长度为 N 的两个序列 A=(A1,…,AN) 和 B=(B1,…,BN),若存在整数 1≤i≤N 满足以下两个条件,则称序列 A 严格字典序小于 序列 B:
- (A1,…,Ai−1)=(B1,…,Bi−1)。
- Ai<Bi。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given a permutation P=(P1,…,PN) of (1,…,N), where (P1,…,PN)=(1,…,N).
Assume that P is the K-th lexicographically smallest among all permutations of (1…,N). Find the (K−1)-th lexicographically smallest permutation.
What are permutations?
A permutation of (1,…,N) is an arrangement of (1,…,N) into a sequence.
What is lexicographical order?
For sequences of length N, A=(A1,…,AN) and B=(B1,…,BN), A is said to be strictly lexicographically smaller than B if and only if there is an integer 1≤i≤N that satisfies both of the following.
- (A1,…,Ai−1)=(B1,…,Bi−1).
- Ai<Bi.
Constraints
- 2≤N≤100
- 1≤Pi≤N(1≤i≤N)
- Pi=Pj(i=j)
- (P1,…,PN)=(1,…,N)
- All values in the input are integers.
The input is given from Standard Input in the following format:
N
P1 … PN
Output
Let Q=(Q1,…,QN) be the sought permutation. Print Q1,…,QN in a single line in this order, separated by spaces.
3
3 1 2
Sample Output 1
2 3 1
Here are the permutations of (1,2,3) in ascending lexicographical order.
- (1,2,3)
- (1,3,2)
- (2,1,3)
- (2,3,1)
- (3,1,2)
- (3,2,1)
Therefore, P=(3,1,2) is the fifth smallest, so the sought permutation, which is the fourth smallest (5−1=4), is (2,3,1).
10
9 8 6 5 10 3 1 2 4 7
Sample Output 2
9 8 6 5 10 2 7 4 3 1
update @ 2024/3/10 11:37:08