UVa299 Train Swapping
来源:互联网 发布:口腔易沟通软件 编辑:程序博客网 时间:2024/06/06 12:29
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 integerL, determining the length ofthe train ( ). The second line of a test case contains a permutation of the numbers1 throughL, indicating the current order of the carriages. The carriages should be orderedsuch that carriage 1 comes first, then 2, etc. with carriageL coming last.
Output Specification
For each test case output thesentence: 'Optimal train swapping takes S swaps.' whereS is an integer.
Example Input
331 3 244 3 2 122 1
Example Output
Optimal train swapping takes 1 swaps.Optimal train swapping takes 6 swaps.Optimal train swapping takes 1 swaps.
这题大意就是要求仅通过相邻的交换将一串数字从小到大排序,这题其实只要写一个排序函数判断两个数是否为相邻,再进行排序,同时记录下函数调用次数,最后输出。
#include <iostream>#include <cstring>using namespace std;const int N = 60;int num[N];int cnt;void my_sort(int *str,int len) {bool tmp = true;while (1) {for (int i = 0; i < len-1; i++) {if (str[i] > str[i+1])tmp = false;}if (tmp)break;for (int i = 0; i < len-1; i++) {if (str[i] > str[i+1]) {cnt++;str[i] = str[i] + str[i+1];str[i+1] = str[i] - str[i+1];str[i] = str[i] - str[i+1];}}tmp = true;}}int main() {int Case;cin >> Case;while (Case--) {int Test;cin >> Test;memset(num,0,sizeof(num));cnt = 0;for (int i = 0; i < Test; i++)cin >> num[i];my_sort(num,Test);cout << "Optimal train swapping takes " << cnt << " swaps." << endl;}return 0;}
- uva299 - Train Swapping
- UVa299 Train Swapping
- uva299 - Train Swapping
- UVA299 Train Swapping
- Uva299——Train Swapping
- Train Swapping
- UVa 299 Train Swapping
- Uva 299 Train Swapping
- uva 299 Train Swapping
- UVA 299 - Train Swapping
- 299 - Train Swapping
- UVA 299-Train Swapping
- UVa 299 - Train Swapping
- uva-299 - Train Swapping
- 299 - Train Swapping
- UVa 299 - Train Swapping
- UVaOJ 299 - Train Swapping
- UVa 299 - Train Swapping
- C++命名空间<转>
- mj评《重返地球》-6分
- mj评《环太平洋》-8.5分
- JQuery学习笔记
- # define NDEBUG assert
- UVa299 Train Swapping
- 应用内转到appstore打分的代码实现
- POJ 3259 Wormholes
- make: *** 警告:文件“Makefile”的修改时间在将来1e+08
- 关于Android中图片compress的有关问题
- Java_SSH三大框架整合
- 14-自定义错误函数-错误处理器
- 用java统计网站在线人数
- SVN服务器搭建和使用(一)