Hair Tying
Time limit1sMemory limit1024 MB
Given values 0 to 3, pick subarrays and overwrite each with its XOR so no 3 remains, then print the minimum number of operations or -1.
- Level
Medium6 of 10
- Topics
- Array, Prefix sum, Dynamic programming
- Solved
- No attempts yet
Problem
Sihyeon likes changing and tying up people's hairstyles. A hairstyle is one of (straight hair), (ponytail), (pigtails), or (three-strand braid).
Given hairstyles , Sihyeon can pick and and set the hairstyles of people through all at once to . is the bitwise XOR operator.
Sihyeon dislikes the oddly shaped three-strand braid, so Sihyeon wants to make sure nobody has a three-strand braid. Find the minimum number of hairstyle changes.
Input
The input has several test cases. The first line contains the number of test cases . ()
The first line of each test case gives . ()
The second line gives the hairstyles , separated by spaces. ()
The sum of over all test cases does not exceed .
Output
For each test case, print on one line the minimum number of hairstyle changes needed so that no three-strand braid remains. If a three-strand braid remains no matter how you change the hairstyles, print .