Codeforces 607B Zuma 【区间dp】

B. 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.

Sample test(s)
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.



题意:给你一个序列,每次可以消除一个回文序列,问你最少需要多少次操作可以删除这个序列。


思路:区间dp,感觉边界状态递推处理有点麻烦,直接上记忆化了。

一般的有 dp[i][j] =min(dp[i][k] + dp[k+1][j]); (i <= k <= j)

特别的有 a[i] == a[j] : dp[i][j] = min(dp[i+1][j-1]);  


AC代码:


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (500+10)
#define MAXM (500000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
int dp[MAXN][MAXN];
int a[MAXN];
int DFS(int i, int j)
{
    if(dp[i][j] != -1) return dp[i][j];
    if(i == j) return 1;
    if(i > j) return 0;
    if(i + 1 == j)
    {
        if(a[i] == a[j])
            return 1;
        else
            return 2;
    }
    dp[i][j] = INF;
    if(a[i] == a[j])
        dp[i][j] = DFS(i+1, j-1);
    for(int k = i; k <= j; k++)
        dp[i][j] = min(dp[i][j], DFS(i, k) + DFS(k+1, j));
    return dp[i][j];
}
int main()
{
    int n; Ri(n);
    for(int i = 1; i <= n; i++)
        Ri(a[i]);
    CLR(dp, -1);
    Pi(DFS(1, n));
    return 0;
}


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