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. Delphi里的RTTI与反射(举例换掉FOnChange)

    Delphi2010之后的RTTI做了很大休整,现在用起来很爽了哦.甚至可以获取某些类的内部私有单元,然后为其赋值!讲这个RTTI增强的,可以参考网上的多个博客内容,我列举一下:Delphi2010R ...

  2. Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤

    原文地址: Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤 - 网络资源是无限的 - 博客频道 - CSDN.NET http://blog.csdn.net/fen ...

  3. SVN中update to revision与revert to revision的区别

    假设我们有许多个版本,版本号分别是1-10 如果我们在7这里选择revert to this version那么7之后的8,9,10的操作都会被消除 如果在7选择revert changes from ...

  4. 《 UNIX网络编程》源码的使用

    学习编程这东西,看代码,改代码,运行代码这样才能学到实际东西!本书说在www.unpbook.com可以获取源码,不过打不开!所以google unpv13e.tar.gz 并在网络上找到了:源码:h ...

  5. 【转】The final local variable xxx cannot be assigned, since it is defined in an enclosing type

    文地址:http://blog.163.com/benben_long/blog/static/199458243201481102257544/ 本文就自己编程时候遇到的一个问题,简要描述一下,并提 ...

  6. Android-xUtils框架介绍(一)

    Android基于快速开发的一个框架——xUtils,它是在aFinal基础上进行重构和扩展的框架,相比aFinal有很大的改善.同时,如果如果你的应用是基于网络的,那么只要处理得当,它会让你彻底的摆 ...

  7. kali获得已经安装的软件列表

    有人说是用dpkg -l ,也有说是用dpkg --get-selections. debian:~# dpkg -l|grep install|wc -l2678debian:~# dpkg --g ...

  8. MediaPlayer中创建AudioTrack的过程

    使用MediaPlayer播放音视频时,会创建AudioTrack对象用于播放音频数据.下面就来看看MediaPlayer创建AudioTrack的过程: 1.创建AudioTrack对象MediaP ...

  9. 浏览器兼容问题系列---使IE支持CSS3 Media Quary

    兼容是一件很让前端攻城师头疼的事情,笔者今天在做一个Demo的时候就碰到了一个问题(大牛就不要拍砖了,谢谢!) 经常做移动互联网前端的攻城师想必对于css3 media quary已经很熟悉了,但是碰 ...

  10. 用TIMESTAMP类型取代INT和DATETIME

    时间在我们开发中应用非常普遍,大部分开发中我们将用Mysql的datetime格式来存储,但是对于经常用时间来排序或者查询的应用中,我们要将时间做成索引,这个就跟查询效率很有关系,但是很多程序员会用i ...