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. 总结一下 IEnumerable 的例子

    本篇将围绕 <试试 IEnumerable 的 10 个小例子>和<试试 IEnumerable 的另外 6 个小例子>给出的例子,总结一下对于 IEnumerable 接口的 ...

  2. Docker学习-Docker搭建Consul集群

    1.环境准备 Linux机器三台 网络互通配置可以参考 https://www.cnblogs.com/woxpp/p/11858257.html 192.168.50.21 192.168.50.2 ...

  3. 016.Kubernetes二进制部署所有节点kube-proxy

    一 部署 kube-proxy kube-proxy 运行在所有节点上,它监听 apiserver 中 service 和 endpoint 的变化情况,创建路由规则以提供服务 IP 和负载均衡功能. ...

  4. js在字符串中加入一段字符串

    在这个功能的实现主要是slice()方法的掌握 arrayObject.slice(start,end) start 必需.规定从何处开始选取.如果是负数,那么它规定从数组尾部开始算起的位置.也就是说 ...

  5. [剑指offer]删除链表中重复的结点(把重复的都删掉,1个不留)

    ①题目 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后为 ...

  6. suseoj 1210: 会场安排问题 (贪心)

    1210: 会场安排问题 时间限制: 1 Sec  内存限制: 128 MB提交: 1  解决: 1[提交][状态][讨论版][命题人:liyuansong] 题目描述 假设要在足够多的会场里安排一批 ...

  7. 在Windows Server 2019通过Docker Compose部署Asp.Net Core

    一.安装Docker Enterprise 安装文档是: https://docs.docker.com/install/windows/docker-ee/ 安装完成后,如下图 二.首先,拉取一个W ...

  8. React源码 React.Children

    children是什么意思呢?就是我们拿到组件内部的props的时候,有props.children这么一个属性,大部分情况下,我们直接把 props.children 渲染到 JSX 里面就可以了. ...

  9. APACHE HADOOP安装

    0.安装前准备 0.1 关闭防火墙 service iptables status service iptables stop 0.2 关闭Selinux 很多稀奇古怪的问题都是SELINUX导致的. ...

  10. day 37 CSS

    参考博客:https://www.cnblogs.com/majj/p/9021419.html 1.CSS的三种链接样式: 行内链接(优先级最高) 内接样式 外接样式 <html lang=& ...