Airport Sort
Time limit2sMemory limit128 MB
For each lineup of zone-grouped tickets, subtract the smallest possible longest simultaneous walk from the minimum adjacent swaps that group the zones.
- Level
Medium7 of 10
- Topics
- Binary search, Greedy, Sorting
- Solved
- No attempts yet
Problem
A few airlines do not assign seat numbers before boarding. Every passenger gets one ticket instead, and the ticket carries a distinct integer in , where is the number of seats on the plane. Ticket numbers are grouped into zones. One zone holds tickets, so numbers through are zone 1, numbers through are zone 2, and the pattern continues. If is not divisible by , the last zone holds fewer than tickets.
Before boarding, the passengers line up in an arbitrary order. Boarding finishes sooner when the first passengers in the line hold zone 1 tickets, the next hold zone 2 tickets, and so on. The order inside a zone does not matter. There are two ways to rearrange the line.
- Two neighbours swap places. Only one adjacent pair can swap per second, and the swapping repeats until the line reaches the required order.
- All passengers walk to their own positions at the same time. Walking from position to position takes seconds. Everybody moves at once, so the line takes as long as the passenger who walks the longest.
The first approach takes more time, but it is less noisy. Your task is to measure how much faster the second one is. Let be the minimum time of the first approach and the minimum time of the second approach, then compute .
Suppose , , and the line reads 3 7 1 2 4 6 5 8 10 9 from the front. Each integer is the ticket number of the passenger standing there, so the passenger at the front holds ticket 3. The passengers holding tickets 7, 2, 5, 10 and 9 are not in their right positions.
The first approach needs at least 6 swaps, which is 6 seconds.
- swap 7 with 1, giving 3 1 7 2 4 6 5 8 10 9
- swap 7 with 2, giving 3 1 2 7 4 6 5 8 10 9
- swap 7 with 4, giving 3 1 2 4 7 6 5 8 10 9
- swap 7 with 6, giving 3 1 2 4 6 7 5 8 10 9
- swap 7 with 5, giving 3 1 2 4 6 5 7 8 10 9
- swap 10 with 9, giving 3 1 2 4 6 5 7 8 9 10, and now everyone stands where they belong.
The second approach takes 5 seconds if the final order is 3 2 1 4 6 5 7 8 9 10. The passengers holding tickets 3, 1 and 8 already stand in the right place, the passengers holding tickets 4, 5, 6, 9 and 10 walk for 1 second, the passenger holding ticket 2 walks for 2 seconds, and the passenger holding ticket 7 walks for 5 seconds. Other final orders also take 5 seconds, and nothing under 5 seconds is possible. So , , and the second approach is 1 second faster.
Input
The first line holds the number of test cases . ()
Each test case takes two lines. The first line holds the positive integers and . (, ) The second line holds distinct integers in . The first integer is the ticket number of the passenger standing at the front of the line.
Output
For each test case, print one line in the form Case x: y, where is the test case number starting at 1 and is how many seconds faster the second approach is, that is .