We have a list of integers . We would like them to be in strictly increasing order, but unfortunately, we cannot reorder them. This means that usual sorting algorithms will not work.
Our only option is to change them by appending digits through to their right (in base ). For example, if one of the integers is , you can turn it into or with a single append operation, or into with two operations (as seen in the image below).
Given the current list, what is the minimum number of single digit append operations that are necessary for the list to be in strictly increasing order?
For example, if the list is , we can use total operations to make it into a sorted list, as the following image shows.
Input Specification
The first line of the input gives the number of test cases, . test cases follow. Each test case is described in two lines. The first line of a test case contains a single integer , the number of integers in the list. The second line contains integers , the members of the list.
Output Specification
For each test case, output one line containing Case #x: y
, where is the test case number (starting from 1) and is the minimum number of single digit append operations needed for the list to be in strictly increasing order.
Limits
Time limit: 10 seconds.
Memory limit: 1 GB.
.
Test Set 1
.
, for all .
Test Set 2
.
, for all .
Sample Input
4
3
100 7 10
2
10 10
3
4 19 1
3
1 2 3
Sample Output
Case #1: 4
Case #2: 1
Case #3: 2
Case #4: 0
In Sample Case #1, the input is the same as in the example given in the problem statement. As the image shows, the list can be turned into a sorted list with 4 operations. Notice that the last two integers need to end up with at least 3 digits (requiring at least 3 append operations in total). If all of the final numbers had exactly three digits, the second would be larger than the third because it starts with a 7 instead of a 1. This means we cannot do it with fewer than 4 operations.
In Sample Case #2, notice that the list needs to be in strictly increasing order, so we have to do at least one operation. In this case, any valid append operation to the second integer works.
In Sample Case #3, we can use two append operations to get the list to .
In Sample Case #4, the given list is already in strictly increasing order, so no operations are necessary.
Comments