Card Game (Large)
Time limit5sMemory limit512 MB
Repeatedly delete neighboring triples in arithmetic progression with difference K to leave as few cards as possible.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Intervals
- Solved
- No attempts yet
Problem
Bob likes card games. At his birthday party his friend Alice gave him a set of cards.
There are cards and each card has one integer written on it. Bob lays the cards on a desk from left to right and wants to discard some of them. Before discarding anything he fixes an integer . Each discard takes three cards that are neighbours at that moment, and if the numbers on those three cards are , and from left to right, they must satisfy
A discarded card leaves no gap, so the cards that were on either side of it become neighbours.
When several triples can be discarded, Bob picks the ones that leave the fewest cards at the end. Find that smallest number of remaining cards.
Input
The first line contains the number of test cases . test cases follow.
Each test case is two lines. The first line contains the number of cards and the number that Bob fixed, separated by a space. The second line contains the numbers written on the cards from left to right, .
Limits
- ()
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 smallest number of cards Bob can be left with after discarding.