시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
1 초 | 16 MB | 45 | 7 | 6 | 35.294% |
Alex has N pets arranged in a straight line. The pets are of three types: cats, dogs and lions. As we all know, cats and dogs do not really like each other, but lions get along with both.
Each move, Alex can take one pet and move it somewhere else in the row.
Alex would like to know the minimum number of moves necessary to rearrange the pets such that there are no cats and dogs next to each other.
Develop a program to calculate the minimum number of moves necessary to rearrange the pets such that there are no cats and dogs next to each other.
The first line of the standard input contains a single integer T representing the number of independent test cases you need to solve.
Each test is represented on two lines of the standard input:
The standard output shall contain a different line for each test case. If there is no way to perform the task print a single integer -1. Otherwise, output the minimum number of moves necessary to rearrange the pets.
2 5 0 1 0 1 2 9 0 0 0 1 1 2 0 0 2
2 1
In the first test case, we move the 2 dogs to the end of the line.
In the second test case, we move the last lion before the first dog