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. PHP 多进程开发

    pcntl_fork(); https://blog.csdn.net/wujiangwei567/article/details/77006724 https://blog.csdn.net/qq_ ...

  2. 累计进度条 PSP 饼图

    累计进度条   PSP   饼图 每周例行报告 本周PSP 类别 任务 开始时间 结束时间 被打断时间 总计工作时间 2016年9月24日 读书 构建之法-6.7章 19:00 20:00 2 58m ...

  3. 命令行方式操作O365

    这几天公司为O365启用了双因子认证,期间出了些小问题,导致我无法使用管理员账号登入控制台,但是Powershell的方法依旧可以.所以我觉得还是有必要把图形化操作改成脚本操作,以备不时之需.以后凡是 ...

  4. .net MVC4 来一个简单的分页代码

    action VodeEntities db = new NewVode.Models.VodeEntities(); ; ) { var sql = "select * ,row_numb ...

  5. UVA11027_Palindromic Permutation

    此题不错.给你一些字字符,要求你用这些字符构成一个回文串,求字典序第k大的回文串. 首先通过给定的字符,我们可以直接判断能否构成回文串(奇数的字符不超过一种),其次可以统计每个字符在回文串的左边应该出 ...

  6. 【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明

    在Java中,可以通过配合调用Object对象的wait()方法和notify()方法或notifyAll()方法来实现线程间的通信.在线程中调用wait()方法,将阻塞等待其他线程的通知(其他线程调 ...

  7. 基于c的简易计算器二

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> ...

  8. rabbitmq 集群安装

    1.安装模式分为三种:单主机模式.普通集群模式.镜像集群模式. 单主机模式:rabbitmq运行在一台主机上,生产环境不建议使用该模式,性能有限.如果该台主机down机,整个服务将不可用. 普通集群模 ...

  9. 【BZOJ4991】我也不知道题目名字是什么(线段树)

    [BZOJ4991]我也不知道题目名字是什么(线段树) 题面 BZOJ 题解 对于线段树维护的区间维护以下东西: 区间左(右)端开始(结束)的最长(短)子串的长度 左端右端的值,以及当前区间内的答案 ...

  10. CentOS 7安装zabbix-2.4.8监控

    说明: 操作系统:CentOS 7 Web环境:Nginx+MySQL+PHP zabbix版本:zabbix-2.4.8.tar.gz 备注:Linux下安装zabbix需要有LAMP或者LNMP运 ...