UVa 331:mapping the Swaps

Source: Internet
Author: User

Topic Link:

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

Topic Type: Backtracking

Original title:

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.

The main effect of the topic:

Given a sequence of numbers, and then you need to exchange two adjacent number of methods, with a minimum number of exchange number of the solution to order how many

Analysis and Summary:

First of all, the bubble sort method must be the least-exchanged solution. So just do a simple brute-force search for all the switching options.

#include <iostream> #include <cstdio> #include <cstring> #define MAXN 7 using 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; }

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.