Painting a fence (small)
InterviewTime limit5sMemory limit512 MB
From up to 10 offers, choose the fewest that cover sections 1 to 10000 using at most 3 distinct colors.
- Level
Medium4 of 10
- Topics
- Brute force, Intervals, Greedy
- Solved
- No attempts yet
Problem
You need to hire people to paint a fence. The fence is 10000 contiguous sections, numbered 1 to 10000.
Painters send you offers. Each offer is to paint one contiguous range of sections in one color. You have to accept a set of the offers so that both of these hold.
- Every section of the fence is painted.
- At most 3 colors are used to paint the fence.
If both conditions can be satisfied, find the minimum number of offers you must accept.
Two accepted offers may overlap. Two colors are the same color only when their strings are equal.
Input
The first line contains the number of test cases .
Each test case is given as follows.
- The first line contains the number of offers .
- The next lines each contain one offer in the form
C A B. is the color, an uppercase letter string of length at most 10. is the first section and is the last section to be painted, with .
Limits
Output
Print one line per test case, in the order the cases are given, in the form Case #X: Y, where is the case number and is the minimum number of offers that must be accepted. If no set of offers satisfies the conditions, print Case #X: IMPOSSIBLE on that line instead.
Hint
These are the five test cases of the sample input.
- In the first case, accepting both offers paints the whole fence, 5000 sections each, with no overlap.
- In the second case the painters overlap, which is allowed.
- In the third case, accepting all four offers covers the whole fence but uses 4 colors, so it does not satisfy the conditions.
- In the fourth case, section 4001 cannot be painted.
- In the fifth case, accepting just the first and the second offer paints the whole fence.