区间dp--洛谷Zuma

4 篇文章 0 订阅
3 篇文章 0 订阅

Zuma

洛谷链接

题面翻译

Genos \texttt{Genos} Genos 最近在他的手机上下载了祖玛游戏。在祖玛游戏里,存在 n n n 个一行的宝石,第 i i i 个宝石的颜色是 C i C_i Ci。这个游戏的目标是尽快的消灭一行中所有的宝石。

在一秒钟, Genos \texttt{Genos} Genos 能很快的挑选出这些有颜色的宝石中的一个回文的、连续的子串,并将这个子串移除。每当一个子串被删除后,剩余的宝石将连接在一起,形成一个新的行列。

你的任务是:求出把整个宝石串都移除的最短时间。

输入格式

第一行包含一个整数 n ( 1 ≤ n ≤ 500 ) n(1 \le n \le 500) n(1n500),表示宝石串的长度。第二行包含 n n n 个被空格分开的整数,第 i ( 1 ≤ i ≤ n ) i(1 \le i \le n) i(1in) 个表示这行中第 i i i 个珠子的颜色。

输出格式

输出一个整数,把这行珠子移除的最短时间。

样例说明

在第一个例子中, Genos \texttt{Genos} Genos 可以在一秒钟就把这行珠子全部移走。在第二个例子中, Genos \texttt{Genos} Genos 一次只能移走一个珠子,所以移走三个珠子花费他三秒。在第三个例子中,为了达到 2 2 2 秒的最快时间,先移除回文串 4   4 \texttt{4 4} 4 4,再移除回文串 1   2   3   2   1 \texttt{1 2 3 2 1} 1 2 3 2 1

感谢 @Administrator2004 提供的翻译

题目描述

Genos recently installed the game Zuma on his phone. In Zuma there exists a line of $ n $ gemstones, the i i i -th of which has color c i c_{i} ci . The goal of the game is to destroy all the gemstones in the line as quickly as possible.

In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?

Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.

输入格式

The first line of input contains a single integer n n n ( 1 < = n < = 500 1<=n<=500 1<=n<=500 ) — the number of gemstones.

The second line contains $ n $ space-separated integers, the i i i -th of which is c i c_{i} ci ( 1 < = c i < = n 1<=c_{i}<=n 1<=ci<=n ) — the color of the i i i -th gemstone in a line.

输出格式

Print a single integer — the minimum number of seconds needed to destroy the entire line.

样例 #1

样例输入 #1

3
1 2 1

样例输出 #1

1

样例 #2

样例输入 #2

3
1 2 3

样例输出 #2

3

样例 #3

样例输入 #3

7
1 4 4 2 3 2 1

样例输出 #3

2

提示

In the first sample, Genos can destroy the entire line in one second.

In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.

In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1.

题目分析

此题由于是求一段序列的状态,可以考虑区间 d p dp dp
d p dp dp仅存在两种转移方式,即当第一个字母是否与最后一个字母相等,很容易得出状态转移方程

d p [ i , j ] = d p [ i + 1 , j − 1 ] dp[i,j]=dp[i+1,j-1] dp[i,j]=dp[i+1,j1]
d p [ i , j ] = m i n ( d p [ i , k ] + d [ k + 1 , j ] , d p [ i , j ] ) dp[i,j]=min(dp[i,k]+d[k+1,j],dp[i,j]) dp[i,j]=min(dp[i,k]+d[k+1,j],dp[i,j])

细节处理

初始化中我们必须初始化长度为2和1的两种取法,并将所有非法状态初始化为正无穷,防止非法转移


code
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=510;
int f[N][N];
int n;
int a[N];
int main()
{
    cin>>n;
    memset(f,0x3f,sizeof f);
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++){
        f[i][i]=1;//初始化长度为1的序列
    }
    for(int i=1;i<n;i++){
        f[i][i+1]=1+(a[i]!=a[i+1]);//初始化长度为2的序列
    }
    for(int len=3;len<=n;len++)//枚举区间长度,1、2我们均已经初始化
        for(int i=1;i<=n-len+1;i++)//枚举左端点
        {
            int j=i+len-1;//计算右端点
            if(a[j]==a[i])f[i][j]=f[i+1][j-1];//第一种状态转移方式
             for(int k=i;k<j;k++){
                f[i][j]=min(f[i][k]+f[k+1][j],f[i][j]);//枚举中间节点进行第二种状态转移
             }
        }
        cout<<f[1][n]<<endl;//输出答案即为区间[1,n]中的最小值
}

  • 25
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值