There is a sequence of 1 6 3 5 4 2 end (one number after the last number is recorded as end)
What is the next arrangement of his Lexicographic Order?
1. we can find the first descending order (x, y) from the back, because [y, end) is already descending, in [y, end) if there is no number in it for exchange, this sequence can be larger. On the contrary, it will become smaller. therefore, [y, end) cannot be larger than [x, end.
That is, 1 63 54 2, (3, 5) indicates the (x, y) pair of this sequence.
2. How can we minimize the value of the [x, end) sequence, that is, "only a little bigger?
We only need to find a number that is a little larger than X and replace it with it, and satisfy this number is the smallest of the optional numbers.
Then we find the first number greater than X as Z, and exchange x Z.
That is, first find 4, 1 63542
Switch 3, 4, 1 6 again4532
The result is: 1 6 4 5 3 2.
3. Finally, the [y, end) sequence must be sorted in descending order. We will change it to a minimum and sort it in descending order so that it can be sorted in ascending order.
That is, after descending order, 1 6 42 3 5
Believe that you are smart, you must have understood -_-