Shortest Straight
Time limit5sMemory limit512 MB
Partition each hand of cards into consecutive runs covering every card so the shortest run is as long as possible.
- Level
Medium6 of 10
- Topics
- Greedy, Binary search, Sorting
- Solved
- No attempts yet
Problem
You are playing a card game. Every card has one integer written on it.
You are dealt a hand of cards, and you have to arrange all of them into straights. A straight is a set of cards whose values are consecutive, for example the three cards {3, 4, 5}, or the single card {7}. No straight holds two cards of the same value, and every card in the hand belongs to exactly one straight.
You are then paid one dollar for every card in the shortest straight you built. An empty hand builds no straights and pays nothing.
For each hand, find the largest amount you can be paid.
Input
The first line contains , the number of hands.
Each of the next lines describes one hand. The line begins with , the number of cards in that hand, followed by the values written on those cards. The numbers on a line are separated by single spaces.
Limits:
- every card value is between and
Output
For each hand print one line of the form Case #x: y, where is the number of the hand counting from 1, and is the largest number of dollars you can be paid.
Notes
In the first hand of the example you hold the ten cards 1 to 10. One straight of length 10 uses all of them and pays 10 dollars.
In the second hand you can build {101, 102, 103, 104, 105, 106} and {103, 104}, which pays 2 dollars. Building {101, 102, 103, 104} and {103, 104, 105, 106} pays 4 dollars instead.
In the third hand you hold no cards, so you are paid nothing.
In the fourth hand the card 9 has no neighbor, so it forms a straight on its own and the shortest straight has length 1.