1683 car restructuring, 1683 restructuring
Blog: doubleq. win
1683 car restructuring
Time Limit: 1 s space limit: 1000 KB title level: Silver QuestionDescription
Description
There is a bridge next to an old railway station, and its bridge deck can rotate horizontally around the bridge pier in the center of the river. Employees at a station can find that the length of the bridge can accommodate up to two carriage lines. If the bridge is rotated 180 degrees, they can switch the positions of the two adjacent carriage lines, this method can be used to re-arrange the carriage order. So he was responsible for using the bridge to sort the railway cars on a small to large basis. After he retired, the railway station decided to automate the work. One of the important tasks was to compile a program, input the initial carriage order, and calculate the minimum number of steps to sort the carriages.
Input description
Input Description
The input file contains two rows of data. The first row is the total number of trains (N (not greater than 10000), and the second row is N different numbers, indicating the initial order of trains.
Output description
Output Description
A piece of data is the minimum number of rotations.
Sample Input
Sample Input
4
4 3 2 1
Sample output
Sample Output
6
1 #include<iostream> 2 using namespace std; 3 int a[10001]; 4 int tot=0; 5 int main() 6 { 7 int n; 8 cin>>n; 9 for(int i=1;i<=n;i++)10 {11 cin>>a[i];12 }13 for(int i=1;i<=n-1;i++)14 {15 for(int j=1;j<=n-1;j++)16 {17 if(a[j]>a[j+1])18 {19 swap(a[j],a[j+1]);20 tot++;21 }22 }23 }24 cout<<tot;25 return 0;26 }