D. Zuma

题目连接:

http://www.codeforces.com/contest/608/problem/D

Description

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 Input

3

1 2 1

Sample Output

1

Hint

题意

给你一个串,你每次可以消去一个回文串

问你最少消去多少次,可以使得这个串清空

题解:

裸的记忆化搜索,比较简单

代码

#include<bits/stdc++.h>
using namespace std;
#define maxn 805int dp[maxn][maxn];
int vis[maxn][maxn];
int a[maxn];
int n;
int dfs(int l,int r)
{if(vis[l][r])return dp[l][r];vis[l][r]=1;dp[l][r]=1e9;if(l>r)return dp[l][r]=0;if(l==r)return dp[l][r]=1;if(l==r-1){if(a[l]==a[r])return dp[l][r]=1;else return dp[l][r]=2;}if(a[l]==a[r])dp[l][r]=dfs(l+1,r-1);for(int i=l;i<=r;i++)dp[l][r]=min(dfs(l,i)+dfs(i+1,r),dp[l][r]);return dp[l][r];
}
int main()
{scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);cout<<dfs(1,n)<<endl;
}

Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索相关推荐

  1. Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索

    A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...

  2. Educational Codeforces Round 52: D. Three Pieces(记忆化搜索)

    D. Three Pieces 题意: 给你一n*n的矩阵,每个格子都有一个数字且所有数字构成一个1~n²的全排列,一开始你的棋子在编号为1的点上,之后你要依次到达编号为2的点.编号为3的点-- 编号 ...

  3. 牛客假日团队赛5 F 随机数 BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 (dfs记忆化搜索的数位DP)...

    链接:https://ac.nowcoder.com/acm/contest/984/F 来源:牛客网 随机数 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  4. Codeforces Round #297 (Div. 2)D. Arthur and Walls 搜索bfs

    题目链接: http://codeforces.com/contest/525/problem/D 题意 给你一个n*m的田地,有一些*的地方是可以移除变成"."的,然后问你移除最 ...

  5. Codeforces Round #459 (Div. 2) C 思维,贪心 D 记忆化dp

    Codeforces Round #459 (Div. 2) C. The Monster 题意:定义正确的括号串,是能够全部匹配的左右括号串. 给出一个字符串,有 (.). ? 三种字符, ? 可以 ...

  6. 思维dp ---- Codeforces Round #711 (Div. 2) - C. Planar Reflections[dp/记忆化搜索]

    题目链接 题目大意: 就是给你n个平面和一个寿命为k的衰变粒子.开始粒子从左向右飞行,粒子每经过一个平面就会产生一个副本粒子,这个副本粒子比原粒子的寿命少1,即为k-1,并且飞行方向是原粒子的反方向. ...

  7. Codeforces Round #439 (Div. 2) C.The Intriguing Obsession(组合数、记忆化搜索)

    题意: 给出a个红色点,b个蓝色点,c个紫色点,我们可以在这些点之间连任意数量长度为1的边,但是限制同颜色的点之间不能连边且同颜色的点之间的最短路径至少为3,求方案数% 998244353, a,b, ...

  8. Codeforces Round #637 (Div. 2) - Thanks, Ivan Belonogov! D. Nastya and Scoreboard题解(记忆化搜索)

    题目链接 题目大意 一个n个数码位的分数板,每一个数码位都是一个七段数码管,现在给出每个数码位的显示情况,问再点亮k段数码管的话能显示的最大的数是多少,如果不能构成一串数字,就输出-1.答案允许有前导 ...

  9. Codeforces Round #797 (Div. 3)无F

    Codeforces Round #797 (Div. 3)无F 这打的也太屎了,白天把G补了才知道简单的很,但f还是没头绪呜呜呜 Problem - A - Codeforces Given the ...

最新文章

  1. 从校园情侣到教授夫妇,520当天他们携手发顶刊!
  2. cap = cv2.VideoCapture(0).read()摄像头读取图片用法
  3. UVa 11168 Airport , 凸包
  4. 信息系统项目管理师-项目采购管理知识点
  5. wcf简单教程(10) ajax调用,wcf简单教程(10) ajax调用
  6. 可执行jar包的MANIFEST.MF
  7. 【MATLAB统计分析与应用100例】案例007:matlab数据的极差归一化变换
  8. 键盘录入,替换敏感词
  9. 初创公司5大Java服务困局,阿里工程师如何打破?
  10. Jquery 安装到Visual Studio 2008
  11. Java 操作 HBase 教程
  12. lorawan服务器通信协议,LoRaWAN协议(三)–Server端数据协议
  13. 【秋招总结】分享我的社招java岗经验,以及我的大厂offer比较(京东网易滴滴,天津农行软开)
  14. 2020年全国大学生数学建模B题题目概要
  15. 9张图看懂AI人工智能现状!从事AI职业女性稀缺!
  16. linux 重启apache:apachectl -k graceful
  17. 2020云栖大会-达摩院
  18. Android中监听电源键长按、Home键、Home键长按
  19. 微信小程序的text 的换行
  20. 英特尔第十代处理器为什么不支持win7_为什么英特尔新推出,i7处理器不支持w7系统,只支持w10...

热门文章

  1. 在C#中调用windows API函数
  2. 贵州发布大数据发展十方面成果
  3. maven学习(中)- 私服nexus搭建
  4. access和SQL的区别
  5. VMware Workstation 与 Device/Credential Guard 不兼容。在禁用 Device/Credenti
  6. C++ 字符ASC排序
  7. Java Web实现分页查询
  8. C语言 用代码将10进制转换为2进制表示
  9. 初识css预处理器:Sass、LESS
  10. maven构建本地jar包到本地仓库