(CodeForces - 607B)Zuma

(CodeForces - 607B)Zuma

time limit per test:2 seconds
memory limit per test:512 megabytes
input:standard input
output:standard output

Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color 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.

Input

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

The second line contains n space-separated integers, the i-th of which is ci (1 ≤ ci ≤ n) — the color of the i-th gemstone in a line.

Output

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

Examples

input

3
1 2 1

output

1

input

3
1 2 3

output

3

input

7
1 4 4 2 3 2 1

output

2

Note

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.

题目大意:对于一个数字序列,每一次攻击可以消灭一个回文串(如果只有一个字符也算一个回文串需要攻击一次),消灭一串回文串后,两端没有消灭的数字序列会接在一起。问最少需要几次攻击可以完全消灭这个数字序列。

思路:题目中提示回文串是最外边两端数字相同,再考虑次外边两端数字相同。因此考虑区间[i,j],以i为研究对象,设f[i][j]表示将区间[i,j]数字全部消去所需要的最少攻击次数。那么最最一般的情况(就是单独一个字符成一个回文串)便是 f[i][j]=f[i+1][j]+1 ,如果两端的数字相等(a[i]==a[j])那么 f[i][j]=min(f[i][j],f[i+1][j1]) ,当然这里i+1可能大于j-1所以需要特判。上述那种情况也可能不是最优解,先消灭中间的回文串会更优,所以枚举k( i<k<j )如果(a[i]==a[k]),那么 f[i][j]=min(f[i][j],f[i+1][k1]+f[k+1][j]) ,同理这里i+1可能大于k-1所以需要特判。

#include
#include
using namespace std;

const int maxn=505;
int a[maxn],f[maxn][maxn];

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",a+i);
            f[i][i]=1;
        }
        for(int len=2;len<=n;len++)
            for(int i=1;i+len-1<=n;i++)
            {
                int j=i+len-1;
                f[i][j]=f[i+1][j]+1;
                if(a[i]==a[j])
                {
                    if(i+1>j-1) f[i][j]=min(f[i][j],1);
                    else f[i][j]=min(f[i][j],f[i+1][j-1]);
                }
                for(int k=i+1;kif(a[i]==a[k])
                    {
                        if(i+1>k-1) f[i][j]=min(f[i][j],1+f[k+1][j]);
                        else f[i][j]=min(f[i][j],f[i+1][k-1]+f[k+1][j]);
                    }
            }
        printf("%d\n",f[1][n]);
    }
    return 0;
}

你可能感兴趣的:(区间dp,codeforces)