Watson and Intervals (Small)
Time limit5sMemory limit512 MB
Generate N intervals from a recurrence, then remove exactly one interval so the number of integers covered by the rest is minimized.
- Level
Medium5 of 10
- Topics
- Intervals, Sorting, Brute force
- Solved
- No attempts yet
Problem
Sherlock and Watson have learned enough C++ in their programming course to move on to algorithmic problems. In today's class the tutor introduced the problem of merging one dimensional intervals. intervals are given, and the th interval is defined by the inclusive endpoints , where .
The tutor defined the covered area of a set of intervals as the number of integers that appear in at least one of the intervals. Formally, an integer contributes to the covered area if there is some with .
Watson likes to challenge Sherlock. This time he asked Sherlock to remove exactly one interval so that the covered area of the remaining intervals is as small as possible. Find that minimum possible covered area after removing exactly one of the intervals.
Input
The first line contains the number of test cases .
Each test case consists of one line with eight integers , , , , , , , and , separated by spaces. is the number of intervals and the first interval is . The other seven values are the parameters used to generate the remaining intervals.
First set and . Then use the recurrences below to generate and for to .
Define and for all to .
Output
For each test case, output one line containing Case #x: y, where is the test case number starting from 1 and is the minimum possible covered area of the intervals remaining after removing exactly one interval.
Constraints
Hint
In the first case of the example, the generation rule produces the single interval . Removing the only interval leaves a covered area of .
In the second case the generated intervals are . Removing the first, second or third interval leaves a covered area of , and respectively.
In the third case the generated intervals are . Removing the first, second, third or fourth interval leaves a covered area of , , and respectively.