UVa 299 Train Swapping: number of bubbles sorted

Source: Internet
Author: User
Tags time limit

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.