# Bzoj1939 [Croatian2010] Zuma

Time Limit: 4 Sec  Memory Limit: 64 MB
Submit: 43  Solved: 31

## Input

The first line of input contains two integers N (1 ≤ N ≤ 100) and K (2 ≤ K ≤ 5) - the number of marbles in the initial sequence and the minimal number of consecutive marbles of the same color he could wish to vanish. The next line contains exactly N integers between 1 and 100 (inclusive), separated by one space. Those numbers represent colors of marbles in the sequence Mirko found.

## Output

The output should contain only one line with a single integer number - the minimal number of marbles Mirko has to insert to achive the desired effect.

## Sample Input

10 4
3 3 3 3 2 3 1 1 1 3

4

## Source

K的限制不定，当K>3的时候，似乎不能见一段消一段了（可能三段拼起来比消两段更优），好像不太容易区间DP？

$f[i][j][k]$表示现在要消除 i ~ j 区间，在i的左边添加了k个珠子。

当k<K-1的时候，我们可以再加一个，也就是 $f[i][j][k]=min(f[i][j][k],f[i][j][k+1]+1)$

当k=K-1的时候，可以消掉i，也就是 $f[i][j][k]=f[i+1][j][0]$

当i和i+1位置的颜色相同的时候，我们可以把i看做合并到i+1，也就是 $f[i][j][k] = f[i+1][j][k+1]$

 1 #include<iostream>
2 #include<algorithm>
3 #include<cstring>
4 #include<cstdio>
5 #include<cmath>
6 using namespace std;
7 const int mxn=105;
9     int x=0,f=1;char ch=getchar();
10     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
11     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
12     return x*f;
13 }
14 int n,K;
15 int a[mxn];
16 int f[mxn][mxn][mxn];
17 int solve(int l,int r,int X){
18     if(l>r)return 0;
19     if(f[l][r][X]!=-1)return f[l][r][X];
20     int &res=f[l][r][X]=0x3f3f3f3f;
21     if(X<K-1)res=min(res,solve(l,r,X+1)+1);
22     if(X==K-1)res=solve(l+1,r,0);
23     for(int i=l+1;i<=r;i++)
24         if(a[i]==a[l])
25             res=min(res,solve(l+1,i-1,0)+solve(i,r,min(K-1,X+1)));
26     return res;
27 }
28 int main(){
29     int i,j;
36 }