299-train swapping
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem &problem=235
At a old railway station, your may still encounter one of the last remaining ' train swappers '. A Train Swapper is a employee of the railroad, whose sole job it are to rearrange the carriages of trains.
Once the carriages are arranged in the optimal order, all train driver-do, are drop the has off, one by on E, at the stations for which the ' load is meant.
The title ' Train Swapper ' stems from the the ' the ' the ' the ' the ' the ' the ' the ' the ' the ' the ' the ' the ' performed ' a station Instead of opening up vertically, the bridge rotated around a pillar in the center of the river. After rotating the bridge of degrees, boats could pass left or right.
The train swapper had discovered this bridge could to operated with at most two on it. By rotating the "bridge" 180 degrees, the carriages switched place, allowing him to rearrange the carriages (as a side effec T, the carriages then faced the opposite direction, but train, can move carriages either, and so who way).
Now this almost all train swappers have died out, and the railway company would like to automate their. Part of the program to being developed, is a routine which decides for a given train the least number of swaps of two T carriages necessary to order the train. Your assignment is to create that routine.
Input specification
The input contains on the ' the ' the number of test cases (N). Each test case consists of two input lines. The A test case contains an integer L, determining the length of the train (
). The second line of a test case contains a permutation of the numbers 1 through L, indicating The carriages. The carriages should is ordered such that carriage 1 comes a, then 2, etc. with carriage L coming.
Output specification
For each test case output the sentence: 'Optimal train swapping takes s swaps.' where s Nteger.
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.
Number of bubbles sorted.