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.

题解:题解:区间DP。若区间i~j为回文串,那么i+1~j-1也必然为回文串,dp[i][j]=dp[i+1][j-1],

如果不是,这就枚举中间点,区间DP裸题;

参考代码为:

 #include<bits/stdc++.h>
using namespace std; int num[], dp[][];
int main()
{
int n;
while (~scanf("%d", &n))
{
memset(num, , sizeof(num));
memset(dp,0x3f,sizeof(dp));
for (int i = ; i <= n; i++)
scanf("%d", &num[i]);
for (int i = ; i <= n; i++)
dp[i][i] = ;
for (int i = n; i >= ; i--)
for (int j = i + ; j <= n; j++)
{
if (num[i] == num[j])
{
if (abs(j - i) != )
dp[i][j] = dp[i + ][j - ];
else
dp[i][j] = ;
} for (int k = i; k<j; k++)
{
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + ][j]);
}
}
printf("%d\n", dp[][n]);
}
return ;
}
 

CodeForces 607B zuma的更多相关文章

  1. Codeforces 607B Zuma(区间DP)

    题目大概说,有n个颜色的宝石,可以消除是回文串的连续颜色序列,问最少要几下才能全部消除. 自然想到dp[i][j]表示序列i...j全部消除的最少操作数 有几种消除的方式都能通过枚举k(i<=k ...

  2. codeforces 607B. Zuma 区间dp

    题目链接 给一个长度为n的序列, 每一次可以消去其中的一个回文串, 问最少几次才可以消完. 代码很清楚 #include <iostream> #include <vector> ...

  3. Zuma CodeForces - 607B (区间DP)

    大意: 给定字符串, 每次删除一个回文子串, 求最少多少次删完. #include <iostream> #include <cstdio> #define REP(i,a,n ...

  4. CodeForces - 607B (记忆化搜索)

    传送门: http://codeforces.com/problemset/problem/607/B Genos recently installed the game Zuma on his ph ...

  5. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

  6. Codeforces Round #336 (Div. 2) D. Zuma

    Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...

  7. 【44.19%】【codeforces 608D】Zuma

    time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  8. Codeforces Round #336 Zuma

    D. Zuma time limit per test:  2 seconds memory limit per test:  512 megabytes input:  standard input ...

  9. Codeforces Round #336 (Div. 2) D. Zuma 区间dp

    D. Zuma   Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gems ...

随机推荐

  1. PHP常用的header头部定义

    <?php header('HTTP/1.1 200 OK'); // ok 正常访问 header('HTTP/1.1 404 Not Found'); //通知浏览器 页面不存在 heade ...

  2. 不需要spring管理,自己根据名字取到对应的bean

    package com.yiban.abc.util; import org.springframework.beans.BeansException; import org.springframew ...

  3. 正则表达式 解决python2升python3的语法问题

      2019.9.12 更新   今天偶然看到 python 官网中,还介绍了一个专门的工具,用于 python2 升级 python3,以后有机会使用下看看 https://docs.python. ...

  4. (Codeforce)The number of positions

    Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say ...

  5. 【algo&ds】0.数据结构和算法入门

    解决问题方法的效率,跟数据的组织方式有关 解决问题方法的效率,跟空间的利用效率有关 解决问题方法的效率,跟算法的巧妙程度有关 什么是数据结构 数据对象在计算机中的组织方式 逻辑结构 物理存储结构 数据 ...

  6. phpStudy中MySQL版本升级到5.7.17方法

    本文主要给大家介绍了关于phpStudy中升级MySQL版本到5.7.17的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧.希望能帮 ...

  7. [ERROR]Unable to locate package

    刚更换了DNS,需要更新下apt-get $ apt-get update

  8. git、git-lab学习记录

    git: 定义:分布式版本控制工具,类似SVN,区别在于SVN如果网络断了,无法进行版本控制,而git是本地进行版本控制,不多bb了,来个图吧 git常用命令: git add 文件          ...

  9. 更新centos7的kernel

    现在安装的centos7 的内核是3.10的, 机器已经联网,可以直接利用包管理工具更新,需要注意的是现在3.0以上的内核引入了签名机制,需要导入签名的key,参考步骤如下: 1.导入keyrpm - ...

  10. inventory

    1.设置主机的默认inventory mode. 2. 设置自动Populate 数据