题干:

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.

题目大意:

给一个数字串,如果是回文串就能相消(类似Zuma游戏),问你要想消除所有的数字,至少需要多少次操作。

解题报告:

跟那道AtCoder - 4244很像,,直接递推。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int a[MAX];
int dp[550][550];
int main()
{int n;cin>>n;for(int i = 1; i<=n; i++) scanf("%d",a+i);memset(dp,INF,sizeof dp);for(int len = 1; len<=n; len++) {for(int l = 1; l<=n-len+1; l++) {int r = l+len-1;if(l == r) dp[l][l] = 1;if(r-l==1) dp[l][r] = 1+(a[l]!=a[r]);else {for(int k = l+1; k<=r; k++) dp[l][r] = min(dp[l][r] , dp[l][k-1] + dp[k][r]);if(a[l] == a[r]) dp[l][r] = min(dp[l][r],dp[l+1][r-1]);}}}printf("%d\n",dp[1][n]);return 0 ;}

【CodeForces - 608D】Zuma(区间dp)相关推荐

  1. BZOJ 1032 JSOI 2007 祖码Zuma 区间DP

    题目大意:依照祖玛的玩法(任意选颜色),给出一段区间.问最少用多少个球可以把全部颜色块都消除. 思路:把输入数据依照连续的块处理.保存成颜色和数量.然后用这个来DP.我们知道,一个单独的块须要两个同样 ...

  2. Financiers Game CodeForces - 737D (博弈论,区间dp)

    大意: 给定$n$元素序列, 两个人从两端轮流拿数, 每一步假设对手上次取k, 那么只能取k或k+1, 先手第一步取1或2, 直到不能拿时停止. 先手要最大化两人数字和的差, 后手要最小化, 求最后差 ...

  3. Codeforces 1025D(区间dp)

    容易想到设f[i][j][k]为i~j区间以k为根是否能构成bst.这样是O(n4)的.考虑将状态改为f[i][j][0/1]表示i~j区间以i-1/j+1为根能否构成bst.显然如果是i-1作为根的 ...

  4. 「Codeforces」598E (区间dp)

    题意:原题在这 有t组输入,每次有n*m的巧克力,要吃k块 只能整行切,代价是长度的平方 求最小代价 做法:(详见行内注释) 枚举切几块和该情况下横切还是纵切 特判: 切0块.没有巧克力.切一整块 暴 ...

  5. CodeForces - 1312E Array Shrinking(区间dp)(通俗易懂)

    CodeForces - 1312E Array Shrinking(区间dp) 题目链接: 没做出来,看了一下别人的题解,才A掉.但网上没发现一篇讲得比较易懂的题解,所以就准备写一篇再加上我自己的理 ...

  6. Codeforces 508E Arthur and Brackets 区间dp

    Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...

  7. CodeForces - 1312E Array Shrinking(区间dp)

    题目链接:点击查看 题目大意:给出 n 个数,现在可执行的操作是: 找到相邻且数值相等的两个数,即 abs( i - j ) == 1 && a[ i ] == a[ j ] 使得两个 ...

  8. Codeforces Round #655 (Div. 2) E. Omkar and Last Floor 区间dp + 巧妙的状态设计

    传送门 题意: 思路: 按照贪心的思路来考虑的话,显然是每一列111的个数越多越好,所以我们能放到一列就放到一列.设f[l][r]f[l][r]f[l][r]为在[l,r][l,r][l,r]内,区间 ...

  9. 【CodeForces - 245H 】Queries for Number of Palindromes (带容斥的区间dp)

    题干: You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. The ...

  10. Codeforces Gym100543L Outer space invaders 区间dp 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF-Gym100543L.html 题目传送门 - CF-Gym100543L 题意 $T$ 组数据. 有 $n ...

最新文章

  1. AOP - PostSharp 2.0
  2. 洛谷-求同构数的个数-NOIP2013提高组复赛
  3. 范灵俊(1983-),男,博士,中国科学院计算技术研究所工程师.
  4. Shell常用参数解释($0、$1、$#、$@、$$、$USER、$HOSTNAME、$LINENO、$RANDOM、$?)
  5. 【赠书】拨云见日 - 深入解析Oracle TX行锁(下)
  6. 刷程序对车危害_刷ecu对车有影响吗?会伤车吗?
  7. html2canvas截图只截取当前可视区域的问题
  8. 【转载】Android 工具-adb原理
  9. mybatis之OGNL表达式
  10. 我的时间管理——任务表
  11. 大学计算机计算题乘法,分数乘法计算题100道
  12. IC基础知识(十五)RS触发器、JK触发器、D触发器、T触发器
  13. Codecademy-中文JavaScript系列教程-初认JS
  14. 计算机系统限制某个用户只允许,Win10系统如何设置某些用户只能运行指定应用程序...
  15. 免费企业邮箱怎么注册申请
  16. win10 格式化 linux u盘,2.win10格式化磁盘和u盘
  17. amd锐龙笔记本cpu怎么样_AMD锐龙R5怎么样 AMD锐龙R5配置参数
  18. 阿里妈妈广告点击转化率(CTR)预估项目(附github代码)
  19. 面经-Bosch博世无锡amp;UL美华
  20. u盘打开之后就只有一个快捷方式

热门文章

  1. 深度学习第三次课-梯度下降与反向传播
  2. [小技巧][Java]Arrays.fill() 初始化 二维数组
  3. 代码 直接调节显示设备亮度_LED显示屏参数系列,亮度是什么,如何调节,如何选择...
  4. mysql定时增量备份_Mysql日常自动备份和增量备份脚本
  5. 7-26 Windows消息队列(25 分)
  6. 如何给ppt编辑页码_拒绝千篇一律:Word插入“侧边”页码,让文档别具一格!...
  7. oracle .dbf文件过大_学习这篇Oracle数据库文件坏块损坏的恢复方法,拓展你的知识面...
  8. pq分解法中b’怎么求_14.初中数学:二元一次方程组,加减消元法怎么解?视频有详细解题步骤...
  9. 石油化工设备维护检修规程_旋回破碎机横梁臂架、衬板、内外铜套检修步骤及设备检修维护要点...
  10. 用计算机求正有理数算术平方根的步骤,用计算器求算数平方根、用有理数估计算数平方根的大小.ppt...