{"trustable":false,"sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003e\r\n\tAt an old railway station, you may still encounter one of the last remaining ``train swappers\u0026#39;\u0026#39;. A train swapper is an employee of the railroad, whose sole job it is to rearrange the carriages of trains.\u003c/p\u003e\r\n\u003cp\u003e\r\n\tOnce the carriages are arranged in the optimal order, all the train driver has to do, is drop the carriages off, one by one, at the stations for which the load is meant.\u003c/p\u003e\r\n\u003cp\u003e\r\n\tThe title ``train swapper\u0026#39;\u0026#39; stems from the first person who performed this task, at a station close to a railway bridge. Instead of opening up vertically, the bridge rotated around a pillar in the center of the river. After rotating the bridge 90 degrees, boats could pass left or right.\u003c/p\u003e\r\n\u003cp\u003e\r\n\tThe first train swapper had discovered that the bridge could be operated with at most two carriages on it. By rotating the bridge 180 degrees, the carriages switched place, allowing him to rearrange the carriages (as a side effect, the carriages then faced the opposite direction, but train carriages can move either way, so who cares).\u003c/p\u003e\r\n\u003cp\u003e\r\n\tNow that almost all train swappers have died out, the railway company would like to automate their operation. Part of the program to be developed, is a routine which decides for a given train the least number of swaps of two adjacent carriages necessary to order the train. Your assignment is to create that routine.\u003c/p\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003cp\u003e\r\n\tThe input contains on the first line the number of test cases (\u003ci\u003eN\u003c/i\u003e). Each test case consists of two input lines. The first line of a test case contains an integer\u0026nbsp;\u003ci\u003eL\u003c/i\u003e, determining the length of the train (\u0026nbsp;\u003cimg align\u003d\"MIDDLE\" alt\u003d\"tex2html_wrap_inline30\" height\u003d\"26\" src\u003d\"http://uva.onlinejudge.org/external/2/299img1.gif\" width\u003d\"83\" /\u003e\u0026nbsp;). The second line of a test case contains a permutation of the numbers 1 through\u0026nbsp;\u003ci\u003eL\u003c/i\u003e, indicating the current order of the carriages. The carriages should be ordered such that carriage 1 comes first, then 2, etc. with carriage\u0026nbsp;\u003ci\u003eL\u003c/i\u003e\u0026nbsp;coming last.\u003c/p\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cp\u003e\r\n\tFor each test case output the sentence: \u0026#39;\u003ctt\u003eOptimal train swapping takes\u0026nbsp;\u003ci\u003eS\u003c/i\u003e\u0026nbsp;swaps.\u003c/tt\u003e\u0026#39; where\u0026nbsp;\u003ci\u003eS\u003c/i\u003e\u0026nbsp;is an integer.\u003c/p\u003e"}},{"title":"Sample Input","value":{"format":"HTML","content":"\u003cpre\u003e\r\n3\r\n3\r\n1 3 2\r\n4\r\n4 3 2 1\r\n2\r\n2 1\u003c/pre\u003e"}},{"title":"Sample Output","value":{"format":"HTML","content":"\u003cpre\u003e\r\nOptimal train swapping takes 1 swaps.\r\nOptimal train swapping takes 6 swaps.\r\nOptimal train swapping takes 1 swaps.\u003c/pre\u003e"}}]}