This page is still under construction.

Parts of this page are still being built. What you see may change.

Painting a fence (small)

Interview

Time limit5sMemory limit512 MB

Summary
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 TT.

Each test case is given as follows.

  • The first line contains the number of offers NN.
  • The next NN lines each contain one offer in the form C A B. CC is the color, an uppercase letter string of length at most 10. AA is the first section and BB is the last section to be painted, with 1≤A≤B≤100001 \le A \le B \le 10000.

Limits

  • 1≤T≤501 \le T \le 50
  • 1≤N≤101 \le N \le 10

Output

Print one line per test case, in the order the cases are given, in the form Case #X: Y, where XX is the case number and YY 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.

Examples1

  1. Example 1

    Input
    5
    2
    BLUE 1 5000
    RED 5001 10000
    3
    BLUE 1 6000
    RED 2000 8000
    WHITE 7000 10000
    4
    BLUE 1 3000
    RED 2000 5000
    ORANGE 4000 8000
    GREEN 7000 10000
    2
    BLUE 1 4000
    RED 4002 10000
    3
    BLUE 1 6000
    RED 4000 10000
    ORANGE 3000 8000
    
    Expected output
    Case #1: 2
    Case #2: 3
    Case #3: IMPOSSIBLE
    Case #4: IMPOSSIBLE
    Case #5: 2