Shortest Straight

Time limit5sMemory limit512 MB

Summary
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 TT, the number of hands.

Each of the next TT lines describes one hand. The line begins with NN, the number of cards in that hand, followed by the NN values written on those cards. The numbers on a line are separated by single spaces.

Limits:

  • 1≤T≤1001 \le T \le 100
  • 0≤N≤10000 \le N \le 1000
  • every card value is between 11 and 1000010000

Output

For each hand print one line of the form Case #x: y, where xx is the number of the hand counting from 1, and yy 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.

Examples3

  1. Example 1

    Input
    4
    10 1 2 3 4 5 10 9 8 7 6
    8 101 102 103 104 105 106 103 104
    0
    5 1 2 3 4 9
    
    Expected output
    Case #1: 10
    Case #2: 4
    Case #3: 0
    Case #4: 1
    
  2. Example 2

    Input
    1
    0
    
    Expected output
    Case #1: 0
    
  3. Example 3

    Input
    3
    1 1
    1 10000
    2 1 10000
    
    Expected output
    Case #1: 1
    Case #2: 1
    Case #3: 1