隐约记得类似的一道JSOI祖玛……然后好像这题不能够把珠子合并成一段?或许是因为这里珠子碰在一起之后可以不消除?

Description

有一行 N 个弹子,每一个都有一个颜色。每次可以让超过 K 个的连续的同颜色的一段 弹子消失,剩下的会重新紧凑在一起。你有无限的所有颜色的弹子,要求在这行弹子中插入 最少的弹子,使得弹子全部消失。

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

Sample Output

4

题目分析

状态$f[i][j][k]$表示从第$i$个珠子到第$j$个珠子这一段,并在$j$后带有$k$个与$j$同色的珠子情况下的答案。

但实际上不甚明白这样表述状态的充要性。

 #include<bits/stdc++.h>
const int maxn = ; int n,m,K,tmp[maxn];
int a[maxn],c[maxn],f[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn]; int read()
{
char ch = getchar();
int num = ;
for (; !isdigit(ch); ch=getchar());
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
return num;
}
inline void Min(int &x, int y){x = x<y?x:y;}
int dp(int l, int r, int t)
{
if (l > r) return ;
if (vis[l][r][t]) return f[l][r][t];
vis[l][r][t] = ;
if (a[r]+t >= K) Min(f[l][r][t], dp(l, r-, ));
else Min(f[l][r][t], dp(l, r, t+1)+1); 
    //这里一开始想成dp(l, r, t-1)+1.实际上这里的状态表示的是逆推。也就是说f[]到完成消除的状态有多远。 
for (int k=l; k<r; k++)
if (c[k]==c[r])
Min(f[l][r][t], dp(l, k, t+)+dp(k+, r-, ));
return f[l][r][t];
}
int main()
{
memset(f, 0x3f3f3f, sizeof f);
n = read(), K = read();
for (int i=; i<=n; i++)
c[i] = read(), a[i] = ;
printf("%d\n",dp(, n, ));
return ;
}

END

最新文章

  1. SOA、ESB、NServiceBus、云计算 总结
  2. 查找数据库中重复的值的数据,having的使用,count(1),sum等聚会函数
  3. selenium webdriver自动化测试
  4. android-studio设置代理
  5. java提高篇(二九)-----Vector
  6. js 通过身份证识别生日、年龄、性别
  7. c++模板类
  8. rotate the clock
  9. HTML5音频,视频播放
  10. vb delphi7、2010 csharp vb.net空白测试程序
  11. 关闭SQL Server 数据库所有使用连接
  12. 转--Windows下将jar包封装成服务程序
  13. PIE使用阴影后的背景透明方法
  14. 成功解决react+webpack打包文件过大的问题
  15. vue day7 table checkbox 全选
  16. Go语言strings和strconv包
  17. java web service 写入图片到web/img/
  18. github构建个人网站模板
  19. Delphi IDE 版本
  20. MyEclipse10 添加反编译JadClipse插件

热门文章

  1. 一个线性表中的元素为整数,设计一个算法,将正整数和负整数分开,使线性表的前一半为负整数,后一半为正整数。(C语言)
  2. Python内建函数二
  3. [Android]XML和JSON的区别
  4. 传智播客C++
  5. h5点击区域和实际区域对不上
  6. 第十九章 排查和调试Web程序 之 防止和排查运行时问题
  7. 【踩坑】springMVC 接收String参数没有判断为空
  8. where whereis locate find 的用法
  9. CF1166C A Tale of Two Lands
  10. 编写Servlet,验证用户登录,如果用户名与密码都为“admin”则验证通过,跳转欢迎页面,否则弹出提示信息“用户名或密码错误,请重新输入!”,点击“确定”后跳转至登录页面