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 ...
随机推荐
- gdb调试多线程程序总结
阿里核心系统团队博客 http://csrd.aliapp.com/?tag=pstack Linux下多线程查看工具(pstree.ps.pstack) http://www.cnblogs.com ...
- Python高级特性
比如构造一个1, 3, 5, 7, ..., 99的列表,可以通过循环实现: 这是正确的姿势
- sqlmap批量扫描burpsuite请求日志记录
sqlmap可以批量扫描包含有request的日志文件,而request日志文件可以通过burpsuite来获取, 因此通过sqlmap结合burpsuite工具,可以更加高效的对应用程序是否存在SQ ...
- Android安全问题 程序锁
导读:本文介绍如何实现对应用加锁的功能,无须root权限 某些人有时候会有这样一种需求,小A下载了个软件,只是软件中的美女过于诱惑与暴露,所以他不想让别人知道这是个什么软件,起码不想让别人打开浏 览. ...
- perl next和last
跳出控制结构:next和last next 和last 操作符运维你在循环中改变程序执行的方向,你可能经常会遇到一些的特殊情况, 碰到这种情况时你希望跳过它,或者像退出循环. 比如当你处理Unix 账 ...
- 【原创】ZYNQ学习笔记(一) HelloWorld实现
拿过ZYNQ开发板,里面给了很多部件,果断从网上下载了手册,N多手册和原理图. 要比Spartan-6复杂多了,耐心地看了看,知道ZYNQ系列分为PS(系统)以及PL(逻辑)部分. 之前,自己一直在做 ...
- poj3270Cow Sorting(置换)
链接 对于组合数学是一点也不了解 讲解 重要一点 要知道一个循环里最少的交换次数是m-1次 . #include <iostream> #include<cstdio> #in ...
- 彻底搞清js中闭包(Closure)的概念
js中闭包这个概念对于初学js的同学来说, 会比较陌生, 有些难以理解, 理解起来非常模糊. 今天就和大家一起来探讨一下这个玩意. 相信大家在看完后, 心中的迷惑会迎然而解. 闭包概念: 闭包就是有权 ...
- php类的实现
zend_class_entry typedef struct _zend_class_entry zend_class_entry; struct _zend_class_entry { char ...
- c#字符串及数组操作
字符串操作(取当前时间)string time=convert.tostring(DateTime.Today).split( new char []{' '}); textbox1.text= ...