Uva331-mapping the Swaps

Source: Internet
Author: User

Mapping the Swaps

Sorting an array can is done by swapping certain pairs of adjacent entries in the array. This was the fundamental technique used in the well-known bubble sort. If We list the identities of the pairs to being swapped, in the sequence they is to is swapped, we obtain what might is call Ed 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 Swappi Ng A1 and A2, and finally swapping A2 and A3. If a pair is identified in a swap map by indicating the subscript of the first element of the pair to being swapped, then thi s sorting process would is characterized with the swap map 2 1 2.

It is instructive to note this there may be many ways in which swapping of adjacent array entries can be used to sort an A Rray. The previous array, containing 3 2 1, could also be sorted by swapping A1 and A2, then swapping A2 and A3, and finally SWA Pping A1 and A2 again. The swap map that describes this sorting sequence is 1 2 1.

For a given array, how many different swap maps exist? A little thought would show that there is an infinite number of swap maps, since sequential swapping of A 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 arrays elements in ascending order. But how many swap maps of minimum size would place 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 a single 0. Each test case would have a integers n that gives the size of an array, and would be followed by the n inte GER values in the array.

Output

For each test case, print a message similar through those shown in the sample 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.

Test instructions: There are several ways to find the minimum number of exchanges by exchanging neighboring elements.

The minimum number of exchanges is the inverse logarithm, which enumerates the exchange schemes.

In addition, the method of bubble sorting must be the least number of swap scenarios.

Solution One: Enumeration of reverse order logarithm reduction scheme, when arriving at the ordered sequence, it must be the minimum number of times of the scheme. time:0.015

#include <cstdio>#include<cstring>#include<iostream>#include<string>#include<algorithm>using namespacestd;Const intMAXN =Ten;intN;intA[MAXN];intans;BOOLIsorder () { for(intI=0; i<n-1; i++)if(a[i]>a[i+1])                 return false; return true;}voidDfsintd) {        if(Isorder ()) {ans++;return; }         for(intI=0; i<n-1; i++)if(a[i]>a[i+1]) {swap (a[i], a[i+1]); DFS (d+1); Swap (A[i], a[i+1]); }}intMain () {#ifndef Online_judge freopen ("./uva331.in","R", stdin);#endif        intKase=0;  while(SCANF ("%d", &n) = =1&&N) { for(intI=0; i<n;i++) scanf ("%d", A +i); Ans=0; if(!isorder ()) DFS (0); printf ("There is%d swap maps for input data set%d.\n", ans, + +Kase); }    return 0;}

Solution Two: First to find the reverse logarithm of D, and then the violent enumeration of the ordering scheme (control depth of D), if the depth of D, the sequence becomes an ordered sequence, we found a minimum number of exchanges. time:0.059

#include <cstdio>#include<cstring>#include<iostream>#include<string>#include<algorithm>using namespacestd;Const intmaxn=Ten;intN;intINV, A[maxn], ans;voidDfsintd) {    if(d==INV) {         for(intI=0; i<n-1; i++)if(a[i]>a[i+1])return; Ans++; } Else  for(intI=0; i<n-1; i++) {swap (a[i], a[i+1]); DFS (d+1); Swap (A[i], a[i+1]); }}intMain () {#ifndef Online_judge freopen ("./uva331.in","R", stdin);#endif    intKase =0;  while(SCANF ("%d", &n) = =1&&N) { for(intI=0; i<n;i++) scanf ("%d", &A[i]); INV=0;  for(intI=0; i<n;i++)             for(intj=i+1; j<n;j++)                if(A[i]>a[j]) inv++; Ans=0; if(Inv >0) DFS (0); printf ("There is%d swap maps for input data set%d.\n", ans, + +Kase); }    return 0;}

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.