Title Address:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_ problem&problem=267
Topic Description:
Sorting an array can do by swapping certain pairs of adjacent in the array. This is the fundamental technique used well-known bubble sort. If We list the identities of the pairs to is swapped, in the sequence they are to be swapped, we obtain what is call Ed a swap map. For example, suppose we wish to sort the array A whose elements are 3, 2, and 1 in. If the subscripts for this array are 1, 2, and 3, sorting the array can is accomplished by swapping A2 and A3, then Swappi Ng A1 and A2, and finally swapping A2 and A3. If a pair is identified into a swap map by indicating the subscript of the ' the ' of the ' pair to be swapped, then thi s sorting process would be characterized with the swap map 2 1 2.
It is instructive to "there may" many ways in which swapping of adjacent array entries can being used to sort a Rray. The previous array, containing 3 2 1, could also is sorted by swapping A1 and A2, then swapping and A2, and finally A3 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'll show this there are an infinite number of swap maps, since sequential swapping of a arbitrary pair The elements won't change the order of the elements. Thus the Swap map 1 1 1 2 1 would also leave our array elements in ascending order. But How many swap maps of minimum size to place a given array in order? That's the question you are to answer in this problem.
Input
The input data would contain an arbitrary number of test cases and followed by a single 0. Each test case would have a integer n that gives the size of a array, and would be followed by the n integer values in the Array.
Output
For each test case, print a message similar to those shown in the sample output below. In no test case Willn is larger than 5.
Sample Input
2 9 7 2 of 3 3 2 1 3 9 1 5 0
Sample Output
There are 1 swap maps for input data set 1.
There are 0 swap maps for input data set 2.
There are 2 swap maps for input data set 3.
There are 1 swap maps for input data set 4.
How many kinds of sorting methods
The search for violence:
Code:
/* UVA Mapping the Swaps 331/#include <stdio.h> #include <stdlib.h> #include <iostream> #include <
string> #include <string.h> #include <algorithm> #define DM 9999999 using namespace std;
int n=0;
int a[5+5]={0};
int as[5+5]={0};
int cases=0;
int sizes=0;
int MINSTEP=DM; /*dfs the sort strategy,the Step's value is the count of the swap*/int dfs (int cur,int step) {if memcmp (As,a,n*sizeo
f (int)) ==0) {if (step>0) {if (step<minstep) {Sizes=1;
Minstep=step;
else if (step==minstep) {sizes++;
}} else {int i=0; for (i=0;i<=n-1-1;i++) {if (a[i]>a[i+1]) {//swap int
Temp=a[i];
A[I]=A[I+1];
A[i+1]=temp;
DFS (cur+1,step+1); Note the InstrucioN ' s sorting a[i+1]=a[i];
A[i]=temp;
}} return (0);
/*for test*/int Test () {while (scanf ("%d", &n)!=eof&&n>0) {cases++;
sizes=0;
MINSTEP=DM;
int i=0;
for (i=0;i<=n-1;i++) {scanf ("%d", &a[i]);
As[i]=a[i];
Sort (as,as+n);
int b[5+5]={1,3,2};
printf ("%d%d\n", memcmp (as,a,n*sizeof (int)), memcmp (as,b,n*sizeof (int));
It ' s not memcmp (as,a,n);
Char *s1= "ABC";
Char *s2= "ACD";
int r=memcmp (s1,s2,3);
return (0);
/*main process*/int Mainproc () {while (scanf ("%d", &n)!=eof&&n>0) {cases++;
sizes=0;
MINSTEP=DM;
int i=0;
for (i=0;i<=n-1;i++) {scanf ("%d", &a[i]);
As[i]=a[i];
Sort (as,as+n);
DFS (0,0); printf ("There are%d SWAP maps for input data set%d.\n ", sizes,cases);
return (0);
int main () {Mainproc ();
Test ();
return (0);
}