[Croatian2010] Zuma(区间DP)

3 篇文章 0 订阅

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

#include<bits/stdc++.h>
#define  maxn 110
using namespace std;
int f[maxn][maxn][maxn],N,K;
int a[maxn];
inline int read()
{
	int rex=0;char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))rex=(rex<<1)+(rex<<3)+(ch&15),ch=getchar();
	return rex;
}
inline int dp(int l,int r,int k)
{
	if(~f[l][r][k])return f[l][r][k];
	if(l>r)return 0;
	f[l][r][k]=1e9;
	if(k==K-1)f[l][r][k]=min(f[l][r][k],dp(l,r-1,0));
	if(k<K-1) f[l][r][k]=min(f[l][r][k],dp(l,r,k+1)+1);
	
	for(int i=r-1;i>=l;i--)
	if(a[r]==a[i])f[l][r][k]=min(f[l][r][k],dp(i+1,r-1,0)+dp(l,i,min(K-1,k+1)));
	return f[l][r][k];
}
int main()
{
	N=read();K=read();
	for(int i=1;i<=N;i++)a[i]=read();
	memset(f,-1,sizeof(f));
	cout<<dp(1,N,0);
	return 0;
}
/**
f[l][r][k] means There are k beads on the right of R and they have the same color
 if  k + 1 == K  --> All K Miss  -->  f[l][r][k] = min f[l][r-1][0]
 if  k + 1 < K  -->  Add One Beat  -->  f[l][r][k] = min f[l][r][k+1] + 1
 loop  i in [ l , r - 1 ]
   -->  if  i and r have the same color
    -->  [ l , i ] and [ r , r ]     and  [ i + 1 , r - 1 ]
    -->  f[l][i][min(K-1,k+1)] + f[i+1][r-1][0]
**/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值