A carriage 1310 through recombinant (bubble sort, similar reverse order)

Description [title]

Next to an old train station there is a bridge, a deck can be rotated around the center of the river piers level. A station workers found the length of the bridge can accommodate up to two cars, if the bridge is rotated 180 degrees, it is possible to exchange the position of two adjacent compartments, this method can rearrange the order of the compartment. He is responsible for using the bridge will stop cabin by cabin number in ascending order. After he retired, the railway station decided to automate this work, one important task is to compile a program, enter the initial carriage order to calculate the minimum number of steps will be able to sort by carriage.

[Enter]

There are two rows of data, the first row of compartments N is the total number (not more than 10,000), a second row of N different numbers indicates the initial order of the compartment.

[Output]

A data, a minimum number of revolutions.

[Sample input]

4
4 3 2 1

[Sample Output]

6 


Because the data range of this problem is really lj, is a water problem, direct two-cycle to get

the code:



#include <the iostream>
#include <cstdio>
#include <the cmath>
#include <CString>
#include <the cstdlib>
#include <algorithm>
the using namespace STD;
int main ()
{
int n-, ANS = 0;
CIN >> n- ;
int [n-] A;
for (int I = 0; I <n-; I ++)
CIN >> A [I];
for (int. 1-n-= I; I> = 0; I -)
for ( int j = 0; j <i ; j ++) // bubble sort must be a positive and negative
{
IF (a [J]> a [+ J. 1]) // direct simulation, if the number is larger than the front of the cabin the latter, once exchanged positions, plus a number of steps
{
the swap (a [J], a [+ J. 1]);
ANS ++;
}
}
COUT << ANS;
return 0;
}

There's really nothing, it can be used as a template title

Guess you like

Origin www.cnblogs.com/57xmz/p/12590393.html
Recommended