天天看点

CF607B Zuma

题目描述

Genos recently installed the game Zuma on his phone. In Zuma there exists a line of nn gemstones, the ii -th of which has color 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 nn ( 1<=n<=5001<=n<=500 ) — the number of gemstones.

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

输出格式

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

题意翻译

题目描述

Genos最近在他的手机上下载了祖玛游戏。在祖玛游戏里,存在n个一行的宝石,第i个宝石的颜色是Ci 。这个游戏的目标是尽快的消灭一行中所有的宝石。 在一秒钟,Genos能很快的挑选出这些有颜色的宝石中的一个回文的,连续的子串,并将这个子串移除。每当一个子串被删除后,剩余的宝石将连接在一起,形成一个新的行列。你的任务是:求出把整个宝石串都移除的最短时间。 让我们给你一个提示:如果一个串正着读或倒着读都一样,那么这个串(或子串)叫回文串。在我们这道题中,“回文”指这个宝石串中的第一个珠子的颜色等于最后一个珠子的颜色,第二个珠子的颜色等于倒数第二个珠子的颜色,等等。 输入输出格式 输入格式:

第一行包含一个整数n(1<=n<=500) ——宝石串的长度。 第二行包含n个被空格分开的整数,第i(1<=i<=n) 个表示这行中第i个珠子的颜色。 输出格式:

输出一个整数,把这行珠子移除的最短时间。 (样例略) 说明:

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

感谢@Administrator2004 提供的翻译

输入输出样例

输入 #1复制

3
1 2 1
      

输出 #1复制

1
      

输入 #2复制

3
1 2 3
      

输出 #2复制

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.

思路:

状态 f[i][j]:消除区间[i,j]最少用的次数。

状态转移方程: f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]); (k是间断点,i<=k<j)

边界值:

区间长度为1: f[i][i]=1;

区间长度为2:当a[i]==a[i+1]  f[i][i+1]=1; 否则 f[i][i+1]=2;

#include <bits/stdc++.h>

using namespace std;

int a[505],f[505][505];
int main()
{
    int n;
    cin>>n;
    for(int i=1; i<=n; i++){
       cin>>a[i];
    }
    memset(f,0x3f,sizeof(f));
    for(int i=1; i<=n; i++){
        f[i][i]=1;//长度为1
        if(a[i]==a[i+1]){//长度为2
          f[i][i+1]=1;
        }
        else f[i][i+1]=2;
    }
    for(int l=3; l<=n; l++){//区间长度从3开始。
        for(int i=1; i+l-1<=n; i++){
           int j=i+l-1;
           if(a[i]==a[j]){
              f[i][j]=f[i+1][j-1];
           }
           for(int k=i; k<j; k++){
              f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]);
           }
        }
    }
    cout<<f[1][n]<<endl;
    return 0;
}