Solution
Explanation
In line with the spirit of range DP, let's define as the minimum number of swipes it takes to clear all the gems between indices and inclusive. If then there's no gems in the range and the array will return .
There's two situations we can transition from.
The first is comparatively simpler, and it's where we erase either the first or first two elements from the array (if they're equal). In this case, the value we get is or .
The second case is a bit nastier, and it may seem a bit unintuitive at first:
If and the gems at positions and are the same color, then we can update with the following candidate value:
You may wonder why there's no extra swipe involved in this transition. After all, where did we clear gems and ?
The answer is that we can clear them along with the last swipe that erased what was left in . Since the last swipe that cleared these gems was a palindrome, we can take on and to the start and end without any problems.
Implementation
Time Complexity:
Java
import java.io.*;import java.util.Arrays;public class Zuma {public static void main(String[] args) throws IOException {BufferedReader read = new BufferedReader(new InputStreamReader(System.in));int gemNum = Integer.parseInt(read.readLine());int[] gems = Arrays.stream(read.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!