传送门:

http://codeforces.com/problemset/problem/607/B

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
  1. 3
    1 2 1
Output
  1. 1
Input
  1. 3
    1 2 3
Output
  1. 3
Input
  1. 7
    1 4 4 2 3 2 1
Output
  1. 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.

分析:

给出n个数字,我们来逐步删除,如果一个子串是回文数,就可以直接删除,问删除完最少的次数。
设dp[i][j]为删除区间[i,j]所需最小次数
 
code:
  1. #include <iostream>
  2. #include<algorithm>
  3. #include <cstdio>
  4. #include<cstring>
  5. using namespace std;
  6. #define max_v 505
  7. int dp[max_v][max_v]={};
  8. int a[max_v];
  9. int dfs(int i,int j)
  10. {
  11. if(i>=j)
  12. return ;
  13. if(dp[i][j])
  14. return dp[i][j];
  15. int ans=1e9;
  16. if(a[i]==a[j])
  17. ans=min(ans,dfs(i+,j-));
  18. for(int k=i;k<j;k++)
  19. ans=min(ans,dfs(i,k)+dfs(k+,j));
  20. return dp[i][j]=ans;
  21. }
  22. int main()
  23. {
  24. int n;
  25. scanf("%d",&n);
  26. for(int i=;i<n;i++)
  27. {
  28. scanf("%d",&a[i]);
  29. }
  30. printf("%d\n",dfs(,n-));
  31. return ;
  32. }

CodeForces - 607B (记忆化搜索)的更多相关文章

  1. Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D) - 记忆化搜索

    Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th nu ...

  2. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  3. Codeforces Gym 100231G Voracious Steve 记忆化搜索

    Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...

  4. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

  5. Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索

    E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...

  6. CodeForces 398B 概率DP 记忆化搜索

    题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...

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

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

  8. Codeforces 148D Bag of mice:概率dp 记忆化搜索

    题目链接:http://codeforces.com/problemset/problem/148/D 题意: 一个袋子中有w只白老鼠,b只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...

  9. codeforces 284 D. Cow Program(记忆化搜索)

    题目链接:http://codeforces.com/contest/284/problem/D 题意:给出n个数,奇数次操作x,y都加上a[x],偶数次操作y加上a[x],x减去a[x],走出了范围 ...

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

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

随机推荐

  1. Jquery判断checkbox选中状态

    jQuery v3.3.1 <input type="checkbox" id="ch"> 判断 $('#ch').is(':checked'); ...

  2. Bzoj1498&1416: [NOI2006]神奇的口袋

    什么鬼双倍经验题??? Sol 考虑在第\(k\)次摸到\(y\)的概率 如果上次摸到\(y\),目前有\(sum\)个球,\(y\)有\(a[y]\)个,那么概率就是\(\frac{a[y]+d}{ ...

  3. 移动端Hybird的网络层优化策略

    一.前端代码策略:域名切换(多域名部署),解决DNS缓存及域名劫持 二.客户端策略 客户端在空闲时ping cdn节点域名列表中的域名,测量延时.丢包等数据.如果延迟 > xxx,丢包 > ...

  4. JS判断是否到达页面底部

    <script type="text/javascript">//判断整个文档到底部$(window).scroll(function(){    //滚动条所在位置的 ...

  5. 19_ThreadLocal

    [概述] 线程局部变量,是一种多线程间并发访问变量的解决方案.与synchronized等加锁的方式不同,ThreadLocal完全不提供锁,而使用以空间换时间的手段,为每个线程提供变量的独立副本,以 ...

  6. Java基础之Map的遍历

    遍历Map集合,有四种方法:   public static void main(String[] args) { Map<String, String> map = new HashMa ...

  7. dubbo学习总结二 服务端

    服务端主要执行对底层数据库的操作 主要分层为 api +dao+ filter+ util+... 首先 dubbo 服务端有一个dubbo配置文件 dubbo:application 定义应用名称 ...

  8. [翻译] Macros with a Variable Number of Arguments - GCC

    可变参数宏(Variadic Macro) 在1999年的ISO C标准中,可以声明一个像函数一样接受可变参数的宏.定义这种宏的语法与函数的定义相似.这是一个例子: #define debug(for ...

  9. IONIC屏幕方向锁定

    如果希望阻止app在设备旋转时发生横屏,可以使用这个插件: cordova plugin add cordova-plugin-screen-orientation // set to either ...

  10. Hyper-V复制

    Hyper-V复制: 默认HV01上的所有虚机都被复制到HV02的 Hyper-V Replica 目录下.虚机启用复制后,当需要启用虚机副本时,要先在HV01上把原虚机关机,然后在HV02上选择故障 ...