Train Swapping 

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

Once the carriages are arranged in the optimal order, all the train driver has to do, isdrop 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 stationclose to a railway bridge. Instead of opening up vertically, the bridge rotated around a pillarin 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 twocarriages on it. By rotating the bridge 180 degrees, the carriages switched place, allowing himto rearrange the carriages (as a side effect, the carriages then faced the opposite direction,but train carriages can move either way, so who cares).

Now that almost all train swappers have died out, the railway company would like toautomate their operation. Part of the program to be developed, is a routine which decidesfor a given train the least number of swaps of two adjacent carriages necessary to order thetrain. 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 oftwo input lines. The first line of a test case contains an integer L, determining the length ofthe train ( ). The second line of a test case contains a permutation of the numbers1 through L, indicating the current order of the carriages. The carriages should be orderedsuch that carriage 1 comes first, then 2, etc. with carriage L coming last.

Output Specification

For each test case output thesentence: '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. 太水了, 可以用冒泡排序的思路, 然后换一次计数一次~ AC代码:
#include<stdio.h>

#define MAXN 50

int N;
int num[MAXN+5];
int main() {
scanf("%d", &N);
while(N--) {
int l;
int count = 0;
scanf("%d", &l); for(int i = 0; i < l; i++)
scanf("%d", &num[i]); for(int i = 0; i < l; i++) {
for(int j = i; j < l; j++) {
if(num[i] > num[j]) {
int t = num[i];
num[i] = num[j];
num[j] = t;
count++;
}
}
}
printf("Optimal train swapping takes %d swaps.\n", count);
}
return 0;
}
												

UVA 299 (13.07.30)的更多相关文章

  1. UVA 10392 (13.07.28)

    Problem F: Factoring Large Numbers One of the central ideas behind much cryptography is that factori ...

  2. UVA 140 (13.07.29)

     Bandwidth  Given a graph (V,E) where V is a set of nodes and E is a set of arcsin VxV, and anorderi ...

  3. UVA 568 (13.07.28)

     Just the Facts  The expression N!, read as `` N factorial," denotes the product of the first N ...

  4. UVA 408 (13.07.28)

     Uniform Generator  Computer simulations often require random numbers. One way to generatepseudo-ran ...

  5. Feb 5 13:07:52 plugh rsyslogd-2177: imuxsock begins to drop messages from pid 12105 due to rate-limiting

    FROM:https://www.nri-secure.co.jp/ncsirt/2013/0218.html SANSインターネットストームセンターのハンドラであるJohannes Ullrichが ...

  6. Saving James Bond - Easy Version 原创 2017年11月23日 13:07:33

    06-图2 Saving James Bond - Easy Version(25 分) This time let us consider the situation in the movie &q ...

  7. UVA 10194 (13.08.05)

    :W Problem A: Football (aka Soccer)  The Problem Football the most popular sport in the world (ameri ...

  8. UVA 253 (13.08.06)

     Cube painting  We have a machine for painting cubes. It is supplied withthree different colors: blu ...

  9. UVA 536 (13.08.17)

     Tree Recovery  Little Valentine liked playing with binary trees very much. Her favoritegame was con ...

随机推荐

  1. perl杂项

    perl -pi -e 's|googleapis.com|useso.com|g' `find ./ -type f` yingc@yingc:~/gcyin/test/thirdparty/ffm ...

  2. Qt之界面数据存储与获取(使用setUserData()和userData())

    在GUI开发中,往往需要在界面中存储一些有用的数据,这些数据可以来配置文件.注册表.数据库.或者是server. 无论来自哪里,这些数据对于用户来说都是至关重要的,它们在交互过程中大部分都会被用到,例 ...

  3. (转) MFC的入口点与消息循环,消息映射

    博文分析的很不错,尤其是替换默认窗口过程的这块,本人觉得,所有的这些都可以参阅侯杰的<深入浅出MFC >. 来自:http://blog.csdn.net/sryan/article/de ...

  4. C# 字符串加密解密方法

    这个是加密的算法的命名空间,使用加密算法前要引用该程序集  System.Security.Cryptography using System;using System.Data;using Syst ...

  5. 【POJ】3415 Common Substrings

    后缀数组可解.使用单调栈优化. /* 3415 */ #include <iostream> #include <sstream> #include <string> ...

  6. 1613. For Fans of Statistics(STL)

    1613 高端的东西 lower_bounder 函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置.如果所有元素都小于val,则返 ...

  7. [原]Unity3D深入浅出 - 粒子系统(Particle System)

    粒子系统是在三维空间渲染出来的二维图像,主要用于烟,火,水滴,落叶等效果.一个粒子系统由粒子发射器.粒子动画器和粒子渲染器三个独立的部分组成. Unity中自带了一些粒子效果,在Assets>I ...

  8. UVa 540 (团体队列) Team Queue

    题意: 每个人都属于一个团体,在排队的时候,如果他所在的团体有人在队伍中,则他会站到这个团体的最后.否则站到整个队伍的队尾. 输出每次出队的人的编号. 分析: 容易看出,长队中,在同一个团体的人是排在 ...

  9. OK335xS Qt network hacking

    /********************************************************************** * OK335xS Qt network hacking ...

  10. 导出Excel帮助类

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.D ...