UVA 299 (13.07.30)
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)的更多相关文章
- UVA 10392 (13.07.28)
Problem F: Factoring Large Numbers One of the central ideas behind much cryptography is that factori ...
- 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 ...
- UVA 568 (13.07.28)
Just the Facts The expression N!, read as `` N factorial," denotes the product of the first N ...
- UVA 408 (13.07.28)
Uniform Generator Computer simulations often require random numbers. One way to generatepseudo-ran ...
- 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が ...
- 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 ...
- UVA 10194 (13.08.05)
:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ...
- UVA 253 (13.08.06)
Cube painting We have a machine for painting cubes. It is supplied withthree different colors: blu ...
- UVA 536 (13.08.17)
Tree Recovery Little Valentine liked playing with binary trees very much. Her favoritegame was con ...
随机推荐
- 关于MIM金属注射成型技术知识大全
1.什么是MIM MIM即(Metal Injection Molding)是金属注射成型的简称.是将金属粉末与其粘结剂的增塑混合料注射于模型中的成形方法.它是先将所选粉末与粘结剂进行混合,然后将混合 ...
- 如何保存ISE综合后的RTL schematic为pdf
如何保存ISE综合后的RTL schematic为pdf 2013-06-23 20:50:10 代码进行综合后,可以得到一个ngr文件,在ISE中打开该文件可以打开RTL schematic,这样每 ...
- JAVA-----乱码的处理 乱码的解决方法总结
为什么说乱码是程序员无法避免的话题呢?这个首先要从编码机制上说起,大家都是中文和英文的编码格式不是一样,解码也是不一样的!工作遇到各种各样的乱码的解决方法总结一下. 对于Java由于默认的编码方式是 ...
- mysqld -install命令时出现install/remove of the service denied错误的原因和解决办法
原因:没有使用管理员权限 解决方法:使用管理员权限打开cmd,然后运行mysqld -install,服务安装成功
- pylinter could not automatically determined the path to `lint.py`
先关闭Sublime Text 1) 到官网先下载pylinter,http://www.logilab.org/project/pylint,然后解压缩,拷贝到C盘,目录为C:\pylint-1.0 ...
- MySQL timed_mutexes
提要: MySQL 5.5.39 Release版本正式从源码里删除了全局参数timed_mutexes.timed_mutexes原本用来控制是否对Innodb引擎的mutex wait进行 计时统 ...
- .net 测试工具类
fluentassertions QuickStart (替换Assert ) https://github.com/dennisdoomen/fluentassertions/wiki Moq ...
- POJ 1273 (基础最大流) Drainage Ditches
虽然算法还没有理解透,但以及迫不及待地想要A道题了. 非常裸的最大流,试试lrj的模板练练手. #include <cstdio> #include <cstring> #in ...
- BZOJ3792: 跑步
题解: 感觉被坑出翔... 显然我们把矩阵乘法中的点当成原图中的边就可以了. 先写opertor 在struct里面居然只能带一个变量?... 放到外面,然后还得加引用? 然后题目描述不清,重边怎么算 ...
- Java [leetcode 19]Remove Nth Node From End of List
题目描述: Given a linked list, remove the nth node from the end of list and return its head. For example ...