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. @resource和@autowired的区别是什么-CSDN论坛-CSDN.NET-中国最大的IT技术社区 - Google Chrome

    @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了.@Resource有两个属性是比较重要的,分 ...

  2. Android Debug 之 Log 最佳实践

    本文微信公众号「AndroidTraveler」首发. 背景 在开发过程中,调试是必不可少的一项工作. 当我们要确定项目的逻辑时,当我们要了解界面的生命周期时,当我们发现新写的逻辑与期望效果不一致时, ...

  3. Material for oauth 2

    oauth 2 in 8 steps:  https://knpuniversity.com/screencast/oauth Live demo of oauth 2 (with server im ...

  4. 如何基于 PHP-X 快速开发一个 PHP 扩展

    0x01 起步 PHP-X本身基于C++11开发,使用cmake进行编译配置.首先,你需要确定所有依赖项已安装好.包括: gcc-4.8 或更高版本 PHP7.0 或更高版本,需要php7-dev 开 ...

  5. 并发编程-硬件加持的CAS操作够快么?

    Talk is cheap CAS(Compare And Swap),即比较并交换.是解决多线程并行情况下使用锁造成性能损耗的一种机制,CAS操作包含三个操作数--内存位置(V).预期原值(A)和新 ...

  6. jenkins手把手教你从入门到放弃01-jenkins简介(详解)

    一.简介 jenkins是一个可扩展的持续集成引擎.持续集成,也就是通常所说的CI(Continues Integration),可以说是现代软件技术开发的基础.持续集成是一种软件开发实践, 即团队开 ...

  7. nyoj 209 + poj 2492 A Bug's Life (并查集)

    A Bug's Life 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Background  Professor Hopper is researching th ...

  8. Mybatis实现数据的增删改查

    Mybatis实现数据的增删改查 1.项目结构(使用maven创建项目) 2.App.java package com.GetcharZp.MyBatisStudy; import java.io.I ...

  9. oracle实现"limit"功能

    转载于http://blog.sina.com.cn/s/blog_67e2758d0100s3oc.html oracle数据库不支持mysql中limit功能,但可以通过rownum来限制返回的结 ...

  10. 磁盘配额管理disk quotas

    条件: a.确保系统内核支持,Linux一般都支持 b.确保分区格式支持,ext2都只持! c.安装有quota软件,centos默认都有! (1)检查内核是否打开磁盘配额支持 [root@cento ...