Ultraviolet A 331-mapping the swaps

Source: Internet
Author: User

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 108 & page = show_problem & problem = 267

Question type: backtracking

Original question:

Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This is the fundamental technique used in the well-known bubble sort. If we list the identities of the pairs
To be swapped, in the sequence they are to be swapped, we obtain what might be called a swap map. for example, suppose we wish to sort the array a whose elements are 3, 2, and 1 in that order. if the Subscripts for this array are 1, 2, and 3, sorting the Array
Can be accomplished by swapping A2 and A3, then swapping 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 be swapped, then this sorting process wocould be characterized
With the swap map 2 1 2.

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

For a given array, how many different swap maps exist? A little thought will show that there are an infinite number of swap maps, since sequential swapping of an arbitrary pair of elements will not change
The order of the elements. thus the swap map 1 1 1 2 1 will also leave our array elements in ascending order. but how many swap maps of minimum size will place a given array in order? That is the question you are to answer in this problem.

Question:

Given a numerical sequence, you need to use the method of exchanging two adjacent numbers to sort the number of solutions with the minimum number of exchanges.

Analysis and Summary:

First, the Bubble sorting method must be the solution with the least number of exchanges. You only need to perform a simple brute-force search to find all the exchange solutions.

#include<iostream>#include<cstdio>#include<cstring>#define MAXN 7using namespace std;bool vis[MAXN], flag;int minSwap,arr[MAXN], n;void swap(int &a,int &b){int t=a; a=b; b=t;}inline bool isAccend(){    for(int i=2; i<=n; ++i)        if(arr[i] < arr[i-1]) return false;    return true;}void dfs(){    if(isAccend()){        ++minSwap;        return;    }    for(int i=1; i<n; ++i){        if(arr[i] > arr[i+1]) {            swap(arr[i], arr[i+1]);            dfs();            swap(arr[i], arr[i+1]);                }            }}int main(){#ifdef LOCAL    freopen("input.txt","r",stdin);#endif    int cas=1;    arr[0] = -99999;    while(scanf("%d", &n), n){        for(int i=1; i<=n; ++i)            scanf("%d", &arr[i]);        minSwap=0;        if(!isAccend())            dfs();        printf("There are %d swap maps for input data set %d.\n", minSwap, cas++);    }    return 0;}

-- The meaning of life is to give it meaning.

 

Original Http://blog.csdn.net/shuangde800 , By d_double(For details, refer)

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.