Editorial for ECOO '13 R2 P4 - Breaking Rocks
Submitting an official solution before solving the problem yourself is a bannable offence.
The Basic Idea
The basic approach here is to generate all the possible ways to split the rock, then check each combination to see if it works. You can use a recursive algorithm to generate all the possible splits, and it's a good idea to only generate the pieces in ascending order because for this problem, 1 1 3
is the same combination as 1 3 1
.
For the example of a
1 1 1 9 1 1 2 8 1 1 3 7
1 1 4 6 1 1 5 5 1 2 2 7
1 2 3 6 1 2 4 5 1 3 3 5
1 3 4 4 2 2 2 6 2 2 3 5
2 2 4 4 2 3 3 4 3 3 3 3
Checking each combination for whether it works is where things get a bit tricky.
An Inefficient Solution
You could use a recursive backtracker to test all the possible ways to place the pieces on the balance. Each piece could be on the left, on the right, or not on the balance at all. For each combination, the difference between the two sides of the balance is the amount of corn you can weigh. In the example above, there are
This approach works, but the problem is that the number of combinations you might have to check grows exponentially (
An Observation
If you start generating and testing splits for any large problem (e.g.
But the bigger pattern that emerges is that the
An Efficient Solution
But if you think more deeply about the structure of the possible splits, it could lead you to an even more efficient solution that would be easily fast enough to complete all of the test data within 30 seconds. Can you figure it out? Or do you have a completely different approach that works? Post your ideas to the appropriate forum at compsci.ca!
It has already been pointed out that there is a
This leads to a much more efficient check on your splits. Instead of using a backtracker to check all the combinations, just make sure that the first piece is of size
Other Solutions?
It seems to me that dynamic programming should be possible when checking splits, though it is unlikely to be as efficient as the approach described above. It also seems to us that there might be a mathematical formula to compute the answer directly given
Comments