The minimum number of interchanges to which a sequence of columns is ordered to increment or decrement.
One. Only neighboring elements can be exchanged
In this case, the minimum number of exchanges is reversed number of numbers, the number of reverse order only to traverse the array from 1~n, each time to add a number to the tree array and then the prefix and can
Two. You can swap elements at any location
For example 2 4 3 1, it can be known that 1 should be exchanged with 2, while 2 should be exchanged with 4, 4 should be exchanged with 1, so that a cycle (3 should be exchanged with itself can be regarded as self-ring), if there is a number of x in such a cycle, as long as the exchange x-1 times can be in the order of the And the different loops are not affected, so if the n number has k cycle, each cycle has the number of AI, then the number of exchanges should be (A1-1) + (a2-1) + ... + (ak-1) = N-k, that is, the number of arrays minus the number of cycles, to calculate the number of loops directly logarithmic set up diagram and then DFS again
The minimum number of exchanges to turn a sequence into an orderly