MarblesRegroupingHard
SRM 387 · 2008-01-09 · by Relja
Problem Statement
John is (as you probably know) a marble collector. He keeps his marbles in boxes. He also likes to keep things in order - in each box there are only marbles of the same color (some boxes may be empty).
One day, his younger brother was playing with the marbles. After he was done, he put all the marbles back in boxes, but he did it randomly, so certain boxes might now contain marbles of different colors. You are given a
Constraints
- boxes will contain between 1 and 50 elements, inclusive.
- Each element of boxes will contain only digits ('0'-'9') and spaces (' ').
- Each element of boxes will be a single space separated list of integers without leading or trailing spaces.
- Each integer in boxes will not contain leading zeros and will be between 0 and 99, inclusive.
- Each element of boxes will contain between 1 and 14 integers, inclusive (that's the number of different colors used).
- All elements of boxes will contain the same number of integers.
- The number of different colors used will be less then or equal to N, where N is the number of elements in boxes.
{"0"}
Returns: 0
{"77 97","8 0"}
Returns: 77
Move all marbles of color 0 to box 1.
{"6 97 7","73 45 0","67 45 63"}
Returns: 170
In the end, all marbles of color 0 are in box 1, all marbles of color 1 are in box 0, and all marbles of color 2 are in box 2.
{"88 55 47 92","0 0 59 0","69 0 61 75","2 94 4 0"}
Returns: 330
{"97 94 0 99","1 72 46 45","0 10 47 75","0 92 76 20","2 25 98 22"}
Returns: 559
Submissions are judged against all 55 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class MarblesRegroupingHard with a public method int minMoves(vector<string> boxes) · 55 test cases · 2 s / 256 MB per case