UVA Mapping the Swaps

Source: Internet
Author: User

The topics are as follows:

Mapping the Swaps

Sorting an array can is done by swapping certain pairs of adjacent entriesin the array. This was the fundamental technique used in the well-knownbubble sort. If We list the identities of the pairs to being swapped, in thesequence they is to is swapped, we obtain what might is Calle d a swap map. For example, suppose we wish to sort the array A whose elements is 3, 2,and 1 in that order. If the subscripts for this array is 1, 2, and 3,sorting the array can be accomplished by swapping A2 and A3, then Swappin G A1and A2, and finally swapping A2 and A3. If a pair is identified in a swapmap by indicating the subscript of the first element of the pair to beswapped and then this Sorting process would is characterized with the swap MAP2 1 2.

It is instructive to note this there may be many ways in which swapping ofadjacent array entries can be used to sort an AR Ray. The previous array,containing 3 2 1, could also be sorted by swapping A1 and A2, then swappingA2 and A3, and finally Swapp ing A1 and A2 again. The swap map that describesthis sorting sequence is 1 2 1.

For a given array, how many different swap maps exist? A Little Thoughtwill show that there is an infinite number of swaps maps, since sequentialswapping of an arbitrary pair of Elements won't change the order of the elements. Thus the Swap map 1 1 1 2 1 would also leave our arrayelements in ascending order. But what many swap maps of minimum size willplace a given array in order? That is, the question you and answer in this problem.

Input

The input data would contain an arbitrary number of test cases, followed by Asingle 0. Each test case would have an integern that gives the size of Anarray, and would be followed by the n intege R values in the array.

Output

For each test case, the print a message similar to those shown in thesample output below. In no test case wouldN be larger than 5.

Sample Input
2 9 72 12 503 3 2 13 9 1 50
Sample Output
There is 1 swap maps for input data set 1.There is 0 swap maps for input data set 2.There is 2 swap maps for input data Set 3.There is 1 swap maps for input data set 4.
For the number of different methods to sort an array, you can exchange the array adjacent to each other, requiring the fewest number of times to be exchanged. In fact, only two elements should not be exchanged repeatedly, the number of exchanges is the least. Once you know this, you can backtrack directly.

The AC code is as follows:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.