Card Game
Time limit5sMemory limit512 MB
Bob repeatedly deletes three neighboring cards whose values form an arithmetic progression with difference K, and seeks the fewest cards that can remain.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Intervals
- Solved
- No attempts yet
Problem
Bob likes playing cards. For his birthday 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 picks one integer . Each discard takes three cards that are neighbours at that moment. If the numbers on those three cards, read from left to right, are , and , they must satisfy
Discarding three cards empties their place, so the card just left of them and the card just right of them become neighbours. Bob repeats this as long as some three neighbouring cards satisfy the condition, and he may choose which three to discard.
Find the smallest number of cards Bob can be left with when he chooses discards as well as possible.
Input
The first line contains the number of test cases . test cases follow.
Each test case has two lines. The first line contains the number of cards and the number that Bob picked, separated by a space. The second line contains the numbers on the cards from left to right, , separated by spaces.
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.