Mod-3 Permutation
InterviewTime limit1sMemory limit128 MB
Count entries by index and value residues mod 3, fix directly swappable pairs first, then resolve each leftover triple with two swaps.
Problem
A permutation of the integers is called mod-3 when holds for every index . For example, 3, 1, 5, 0, 4, 2 is a mod-3 permutation, while 1, 2, 0, 4, 5, 3 is not.
You are given one permutation. In a single step you can pick two different indices and swap the values at those two positions. Find the minimum number of steps that turns the given permutation into a mod-3 permutation.
Input
The first line contains the number of test cases , where .
Each test case takes two lines. The first line contains an integer , and the second line contains integers separated by single spaces. Those integers are a permutation of . satisfies and is always a multiple of 3.
Output
For each test case, print one line in the form Case #x: M, where x is the test case number starting from 1 and M is the minimum number of swaps that turns the given permutation into a mod-3 permutation.