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 805 int 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. Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索

    https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...

  3. Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)

    题意 在一个有向无环图上,两个人分别从一个点出发,两人轮流从当前点沿着某条边移动,要求经过的边权不小于上一轮对方经过的边权(ASCII码),如果一方不能移动,则判负.两人都采取最优策略,求两人分别从每 ...

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

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

  5. 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 ...

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

    题目链接:https://codeforces.com/contest/608/problem/D 题意:给出n个宝石的颜色ci,现在有一个操作,就是子串的颜色是回文串的区间可以通过一次操作消去,问最 ...

  7. codeforces 793 D. Presents in Bankopolis(记忆化搜索)

    题目链接:http://codeforces.com/contest/793/problem/D 题意:给出n个点m条边选择k个点,要求k个点是联通的而且不成环,而且选的边不能包含选过的边不能包含以前 ...

  8. 牛客假日团队赛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 ...

  9. Codeforces Gym 191033 E. Explosion Exploit (记忆化搜索+状压)

    E. Explosion Exploit time limit per test 2.0 s memory limit per test 256 MB input standard input out ...

随机推荐

  1. Effective java笔记4--方法

    一.检查参数的有效性 极大多数方法和构造函数都会对于传递给它们的参数值有某些限制. 对于公有的方法,使用Javadoc @throws标签(tag)可以使文档中记录下“一旦针对参数值的限制被违反之后将 ...

  2. MongoDB之一介绍(MongoDB与MySQL的区别、BSON与JSON的区别)

    MySQL与MongoDB的操作对比,以及区别 MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL ...

  3. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 unselect_from_list_by_index(self, locator, *indexes)

    def unselect_from_list_by_index(self, locator, *indexes): """Unselects `*indexes` fro ...

  4. Raspberry Pi3 ~ 搭建开发环境

    关于树莓派的开发环境 纠结了一些时间 ,我的是raspberry Pi 3 mode b 在官网下载 noobs (raspbain 版本)的. 安装完成之后接上显示器 启动系统 然后最初我是想在这个 ...

  5. html --- canvas --- javascript --- 绘图方法

    Canvas元素是HTML5的一部分,允许脚本语言动态渲染位图像. 如有疑问请访问链接:http://javascript.ruanyifeng.com/htmlapi/canvas.html < ...

  6. C++抽象机制之二:运算符重载

    1.二元运算符aa@bb,可以定义为 1).一个参数的非静态成员函数:aa.operator@(bb);  (成员函数有this指针) 2). 两个参数的非成员函数:operator@(aa,bb); ...

  7. MySql 5.6 查询日志

    记录所有查询的方法 打开My.ini 在 [mysqld]下面写 general_log= ON; log_output= TABLE; 然后保存重启mysql 最后在mysql库下 general_ ...

  8. Problem About Salesforce SOAP API 32.0 In .Net Project

    最近在集成项目项目中遇到一个问题:在用最新版本(API 32.0)Enterprise WSDL在.Net 中做集成时,初始化SforceService 时会初始化类错误.这算是Salesforce ...

  9. focus与定位

    <html> <head> <script type="text/javascript" src="/jquery/jquery.js&qu ...

  10. URAL-1981 Parallel and Perpendicular 水题

    题目链接:http://www.cnblogs.com/zhsl/p/3395868.html 水题,注意细节. //STATUS:C++_AC_31MS_333KB #include <fun ...