zoukankan      html  css  js  c++  java
  • Uva 299 Train Swapping

    Train Swapping 

    At an old railway station, you may still encounter one of the last remaining ``train swappers''. A train swapper is an employee of the railroad, whose sole job it is to rearrange the carriages of trains.

    在古老的火车站,你可能还会遇到遗存的“列车转置员”,列车转置员是在铁路上工作的一名员工,他的唯一作用就是重排列车的车厢

    Once the carriages are arranged in the optimal order, all the train driver has to do, is drop the carriages off, one by one, at the stations for which the load is meant.

     一旦列车的车厢被安排为最理想的顺序,所有的列车驾驶员需要做的就是将车厢按顺序在站台上开出

    The title ``train swapper'' stems from the first person who performed this task, at a station close to a railway bridge. Instead of opening up vertically, the bridge rotated around a pillar in the center of the river. After rotating the bridge 90 degrees, boats could pass left or right.

    列车转置员”源于第一个干这工作的人,在靠近铁路桥梁的站台上,桥架不是垂直建立的,相比之下而是围绕在河中心的一根柱子展开的,当转动桥架九十度后,船可以从左边或右边穿过。

    The first train swapper had discovered that the bridge could be operated with at most two carriages on it. By rotating the bridge 180 degrees, the carriages switched place, allowing him to rearrange the carriages (as a side effect, the carriages then faced the opposite direction, but train carriages can move either way, so who cares).

    第一个列车转置员发现桥架最多可以操作两节车厢,通过旋转桥架180度,两节车厢可以交换各自的位置,这样可以允许他重置车厢,(有一个副作用,这时车厢面对着不同的方向,但是列车车厢两边都可以移动,谁在乎呢!)

    Now that almost all train swappers have died out, the railway company would like to automate their operation. Part of the program to be developed, is a routine which decides for a given train the least number of swaps of two adjacent carriages necessary to order the train. Your assignment is to create that routine.

     现在,大多数列车转置员都已经去世了,但铁道公司现在想运行这个系统,一部分系统需要建立,这个系统的程序是计算给定的一辆列车在达到相邻车厢符合列车要求下最少的转换次数,你的任务就是写出这个程序。

    Input Specification

    The input contains on the first line the number of test cases (N). Each test case consists of two input lines. The first line of a test case contains an integer L, determining the length of the train ( tex2html_wrap_inline30). The second line of a test case contains a permutation of the numbers 1 through L, indicating the current order of the carriages. The carriages should be ordered such that carriage 1 comes first, then 2, etc. with carriage L coming last.

    Output Specification

    For each test case output the sentence: 'Optimal train swapping takes S swaps.' where S is an integer.

    Example Input

    3
    3
    1 3 2
    4
    4 3 2 1
    2
    2 1

    Example Output

    Optimal train swapping takes 1 swaps.
    Optimal train swapping takes 6 swaps.
    Optimal train swapping takes 1 swaps.


    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
        int cari[52], n, T, count, i, j, flag, temp;
        scanf("%d", &T);
        while(T--)
        {
            scanf("%d", &n);
            count = 0;
             for(i=0; i<n; ++i) scanf("%d", &cari[i]);
             for(i=0; i<n-1; i++)
             {
                 flag = 1;
                 for(j=0; j<n-1-i; ++j)
                 {
                     if(cari[j] > cari[j+1])
                     {
                         flag = 0;
                         temp = cari[j], cari[j] = cari[j+1], cari[j+1] = temp;
                         count++;
                     }
                 }
                 if(flag) break;
             }
             printf("Optimal train swapping takes %d swaps.\n", count);
             count = 0;
        }
        return 0;
    }

    解题思路:

    第一次看题目,不好意思没看懂,前面一大堆废话,但还是有牵涉到题目的地方,为了让自己能够理解题意,不得不一个一个翻译过来,最后……

    要求是统计冒泡排序中swap的次数



  • 相关阅读:
    Flume实现写入es
    JMeter创建上传文件脚本
    JQuery的dataTable实现分页
    Dubbo服务发布机制-源码学习
    spring容器启动过程(Spring源码阅读)
    Hadoop学习笔记一(HDFS架构)
    hbase修改表TTL
    hive复制表
    提交docker镜像到远程仓库
    centos7 安装ssh
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/2843359.html
Copyright © 2011-2022 走看看