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. 配置高可用集群(实验) corosyne+pacemaker

    环境准备: 一准备三个虚拟机,把/etc/hosts/文件配置好                              192.168.43.9 node0                     ...

  2. 『编程题全队』Alpha 阶段冲刺博客Day4

    1.每日站立式会议 1.会议照片 2.昨天已完成的工作统计 孙志威: 1.添加团队界面下的看板容器SlotWidget 2.实现SlotWidgets的动态布局管理 3.实现团队/个人界面之间的切换 ...

  3. lr常见问题汇总(持续更新版)

    在使用mms协议的时候,会碰到报错 Action.c(): 错误: C 解释器运行时错误: Action.c (): Error -- Unresolved symbol : mms_play. 解决 ...

  4. PHP数据库常用常量笔记

    参考:http://php.net/manual/zh/pdo.constants.php Warning 自 PHP 5.1 起,开始使用类常量.以前的版本使用类似 PDO_PARAM_BOOL 这 ...

  5. Oracle与SQLSERVER 批处理执行 DDL 语句

    1. 公司里面的 很多同名的数据库 的一个表都错误的多了一个列 要是每个都用数据库连接工具打开 感觉太废时间了. 比如写个sql命令来执行. 具体方法: Oracle 使用 sqlplus sqlpl ...

  6. mysql索引的优化

    MySQL索引的优化 上面都在说使用索引的好处,但过多的使用索引将会造成滥用.因此索引也会有它的缺点:虽然索引大大提高了查询速度,同时却会降低更新表的速度,如对表进行INSERT.UPDATE和DEL ...

  7. testdisk修复文件系统

    故障修复步骤: 1. 检查磁盘分区级文件系统确实不在: 2. 云主机内部下载testdisk工具修复 yum install testdisk -y 3. 执行命令testdisk /dev/vdc进 ...

  8. 用java和junit编写app自动化测试用例

    package myTest; import static org.junit.Assert.*; import io.appium.java_client.android.AndroidDriver ...

  9. Pscp与服务器文件传递

    1. 下载pscp.exe https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html 选择并下载pscp.exe 2. 执行cmd程 ...

  10. NOIP赛前集训营-提高组(第一场)#B 数数字

    题目描述 小N对于数字的大小一直都有两种看法.第一种看法是,使用字典序的大小(也就是我们常用的判断数字大小的方法,假如比较的数字长度不同,则在较短一个前面补齐前导0,再比较字典序),比如43<3 ...