BoxesArrangement
SRM 351 · 2007-05-29 · by Andrew_Lazarev
Problem Statement
Several boxes are placed in a row. Each box is one of three colors. We want to rearrange these boxes in a such way that no three consecutive boxes are of the same color. This process should affect the fewest boxes possible. More formally, we want to achieve the desired configuration by swapping pairs of boxes, and we want to maximize the number of boxes that are never moved.
You will be given a
Constraints
- boxes will contain between 1 and 50 characters, inclusive.
- boxes will consist of characters 'A', 'B' and 'C' only.
"AAABBBCCC" Returns: 6
The boxes could be rearranged into "ABABCBCAC".
"AAAAAAAACBB" Returns: 7
The best rearangement is "AABAABAACAA".
"CCCCCB" Returns: -1
"BAACAABAACAAA" Returns: 5
"BCBBBACBBBBBBACCBB" Returns: 12
Submissions are judged against all 117 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class BoxesArrangement with a public method int maxUntouched(string boxes) · 117 test cases · 2 s / 256 MB per case