The number of solutions for the exchange of 331 of the ultraviolet

Source: Internet
Author: User

Exchange two adjacent elements of an array to sort the array. This function is similar to Bubble sorting. However, there may be more than one exchange scheme. For example, if array a [3] is 3, 2, 1, and you want to rank it 1, 2, 3, you can first swap the elements of positions 1 and 2 (the array is changed to 2, 3, 1 ), then, the elements of positions 2 and 3 (changed to 2, 1, 3) are exchanged, and the elements of positions 1 and 2 (changed to 1, 2, 3) are finally exchanged. This is solution 1. For details, 1, 2, 1 (number is the position of the first number of each switch. Of course, you can switch positions 2 and 3 (312) first, then switch positions 1 and 2 (132) and finally switch positions 2 and 3 (123). The above description method is 2, 1, 2. This is solution 2. Of course, this solution is infinite. For example, and 2 can also be sorted. However, in this case, the first two exchanges are useless, not the solution with the smallest number of moves. This question requires the minimum number of moves. In this example, there are only two solutions.

 

 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cctype> 4 #include <cstring> 5 #include <cmath> 6 #include <ctime> 7 #include <string> 8 #include <vector> 9 #include <map>10 #include <set>11 #include <algorithm>12 #include <iostream>13 using namespace std;14 typedef long long ll;15 const int NO = 10000 + 10;16 int a[NO];17 int n, ans;18 19 inline bool judge()20 {21     for( int i = 1; i < n; ++i )22         if( a[i] > a[i+1] )23             return false;24     return true;25 }26 27 int Swap( int i, int j )28 {29     int t = a[i];30     a[i] = a[j];31     a[j] = t;32 }33 34 void dfs()35 {36     if( judge() )37     {38         ++ans;39         return;40     }41     for( int i = 1; i < n; ++i )42         if( a[i]  > a[i+1] )43         {44             Swap( i, i+1 );45             dfs();46             Swap( i, i+1 );47         }48 }49 50 int main()51 {52     int T = 0;53     while( ~scanf( "%d", &n ) && n )54     {55         for( int i = 1; i <= n; ++i )56             scanf( "%d", &a[i] );57         ans = 0;58         if( !judge() )59             dfs();60         printf( "There are %d swap maps for input data set %d.\n", ans, ++T );61     }62     return 0;63 }
View code

 

The number of solutions for the exchange of 331 of the ultraviolet

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.