codevs 1683 车厢重组

1683 车厢重组

 时间限制: 1 s
 空间限制: 1000 KB
 题目等级 : 白银 Silver
 
题目描述 Description

在一个旧式的火车站旁边有一座桥,其桥面可以绕河中心的桥墩水平旋转。一个车站的职工发现桥的长度最多能容纳两节车厢,如果将桥旋转180度,则可以把相邻两节车厢的位置交换,用这种方法可以重新排列车厢的顺序。于是他就负责用这座桥将进站的车厢按车厢号从小到大排列。他退休后,火车站决定将这一工作自动化,其中一项重要的工作是编一个程序,输入初始的车厢顺序,计算最少用多少步就能将车厢排序。

输入描述 Input Description

输入文件有两行数据,第一行是车厢总数N(不大于10000),第二行是N个不同的数表示初始的车厢顺序。

输出描述 Output Description

一个数据,是最少的旋转次数。

样例输入 Sample Input

4

4 3 2 1 

样例输出 Sample Output

6

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio> 
using namespace std;
int a[10000000],n,tot;
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<n;i++)
        for(int j=0;j<n-1;j++)
            if(a[j]>a[j+1])
            {
                swap(a[j],a[j+1]);
                tot++;
            }
    printf("%d",tot);
    return 0;
}
原文地址:https://www.cnblogs.com/kuaileyongheng/p/6719010.html