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

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

HINT

 

Source

动态规划 区间DP

参考了这里的题解 http://blog.csdn.net/u014609452/article/details/63267943

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

大力脑洞一下发现还是可以DP的

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

共有三种决策(@游戏王v6):

  当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] $

转移有些零散,写成记忆化搜索的形式会很方便。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,K;
int a[mxn];
int f[mxn][mxn][mxn];
int solve(int l,int r,int X){
if(l>r)return ;
if(f[l][r][X]!=-)return f[l][r][X];
int &res=f[l][r][X]=0x3f3f3f3f;
if(X<K-)res=min(res,solve(l,r,X+)+);
if(X==K-)res=solve(l+,r,);
for(int i=l+;i<=r;i++)
if(a[i]==a[l])
res=min(res,solve(l+,i-,)+solve(i,r,min(K-,X+)));
return res;
}
int main(){
int i,j;
n=read();K=read();
for(i=;i<=n;i++)a[i]=read();
memset(f,-,sizeof f);
solve(,n,);
printf("%d\n",f[][n][]);
return ;
}

Bzoj1939 [Croatian2010] Zuma的更多相关文章

  1. 【动态规划】bzoj1939: [Croatian2010] Zuma

    隐约记得类似的一道JSOI祖玛……然后好像这题不能够把珠子合并成一段?或许是因为这里珠子碰在一起之后可以不消除? Description 有一行 N 个弹子,每一个都有一个颜色.每次可以让超过 K 个 ...

  2. 【BZOJ1939】[Croatian2010] Zuma(动态规划)

    题目: BZOJ1939(权限题) 分析: 这题很容易看出是DP,但是状态和转移都不是很好想-- 用\(dp[l][r][c]\)表示在\(l\)前面已经新加了\(c\)个和\(l\)一样的弹子时,使 ...

  3. 「SPOJ6340」「BZOJ1939」ZUMA - ZUMA【记忆化搜索】

    题目链接 [洛谷传送门] 题解 \(f[i][j][k]\)表示在消除了\((i,j)\),在后面加上了\(k\)个珠子的总的珠子数. 考虑三种决策:(题目给出的\(k\)在下文表示成\(K\)) 决 ...

  4. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  5. bzoj1032 [JSOI2007]祖码Zuma

    1032: [JSOI2007]祖码Zuma Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 672  Solved: 335[Submit][Stat ...

  6. Codeforces Round #336 Zuma

    D. Zuma time limit per test:  2 seconds memory limit per test:  512 megabytes input:  standard input ...

  7. Codeforces Round #336 (Div. 2) D. Zuma 区间dp

    D. Zuma   Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gems ...

  8. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

  9. Codeforces Round #336 (Div. 2) D. Zuma

    Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...

随机推荐

  1. CXGRID用法(取行、列值;定位选中某行等等)[转]

    Delphi Cxgrid获取选中行列,排序规则,当前正在编辑的单元格内的值 cxGrid1DBTableView1.Controller.FocusedRowIndex 当前行号 cxGrid1DB ...

  2. Linux限制cpu睿频&限制频率

    .关闭睿频 > /sys/devices/system/cpu/intel_pstate/no_turbo .限制CPU最大频率到50% " | sudo tee /sys/devic ...

  3. ajax 数据请求(一)同域

    参考:http://www.css88.com/jqapi-1.9/jQuery.ajax/ http://www.cnblogs.com/haitao-fan/p/3908973.html 1.常用 ...

  4. MVC框架理解及优缺点

    MVC:模型(Model).视图(View).控制器(Control) 流程如下: 首先是视图向控制器发送请求,控制器再将业务请求发送给模型, 然后模型处理请求业务并把结果返回给控制器,控制器根据结果 ...

  5. 小程序 获取用户的openid

    wx.login({ success: res => { var code = res.code; //返回code // 小程序appid var appId = 'wxd751fc845c9 ...

  6. ASP.NET MVC中在 @RenderBody() 或者 @Html.Partial()中需要使用引入外部js,css

    今天想在后台封装一下bootstraptree这个插件,引入jquery.js bootstrap.js bootstrap.css bootstrap-tree.js后,我在页面查看脚本错误就连最简 ...

  7. 第219天:Angular---过滤器

    在Angular中,过滤器的功能主要是格式化数据表达式,且可以自定义过滤器.作用域(scope)主要服务于页面模板,在控制器和页面中起桥梁作用,保存模板中的数据对象,为模板中的元素提供方法和属性. 一 ...

  8. ldap禁止匿名用户登录

    此处默认ldap已经安装完成,安装文档传送门:https://www.cnblogs.com/crysmile/p/9470508.html openldap默认安装完成,是允许匿名用户登录的,因此需 ...

  9. [UVA 10635] Prince ans Princess

    图片加载可能有点慢,请跳过题面先看题解,谢谢 这道题... 还是要点思维的... 第一眼看是个最长公共子序列,但是, \(N\le 62500\) ,并不能 \(O(n^2)\) 求 $ $ 这道题有 ...

  10. 《Linux内核设计与实现》学习总结 Chap18

    一.准备开始 1.一个确定的bug,但大部分bug通常都不是行为可靠且定义明确的. 2.一个藏匿bug的内核版本. 3.相关内核代码的知识和运气. 二.内核中的bug 1.bug的表象: 明白无误的错 ...