Dire Straights (Small)
InterviewTime limit5sMemory limit512 MB
Split the hand into groups of consecutive values to make the shortest group as long as possible.
- Level
Medium5 of 10
- Topics
- Backtracking, Sorting
- Solved
- No attempts yet
Problem
You are playing a card game where every card has one integer written on it.
At the start you receive some cards, your hand. You then split the whole hand into straights, leaving no card out. A straight is a set of cards with consecutive values, for example the three cards {3, 4, 5} or the single card {7}. Once the hand is split you receive a number of dollars equal to the length of the shortest straight. With no cards in hand you can form no straight, so you receive 0 dollars.
Each test case describes the cards in your hand. Find the largest number of dollars you can receive in each test case.
Input
The first line contains the number of test cases . Each test case is one line. Each line contains , the number of cards in the hand, followed by the integers written on those cards, all separated by spaces.
Limits
- The numbers on the cards are between 1 and 10000.
Output
For each test case print one line in the form Case #x: y, where is the test case number starting from 1 and is the largest number of dollars you can receive in that test case.
Hint
In the first case of the sample input you hold ten cards numbered 1 to 10. One straight of length 10 covers them and pays 10 dollars.
Splitting the second case into {101, 102, 103, 104, 105, 106} and {103, 104} pays 2 dollars. Splitting it into {101, 102, 103, 104} and {103, 104, 105, 106} pays 4 dollars.
The third case has no cards, so it pays 0 dollars.
In the fourth case the card numbered 9 has to sit in a straight of its own, so the answer is 1 dollar.