CodeForces - 607B (记忆化搜索)
传送门:
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
3
1 2 1
1
3
1 2 3
3
7
1 4 4 2 3 2 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.
分析:
#include <iostream>
#include<algorithm>
#include <cstdio>
#include<cstring>
using namespace std;
#define max_v 505
int dp[max_v][max_v]={};
int a[max_v];
int dfs(int i,int j)
{
if(i>=j)
return ;
if(dp[i][j])
return dp[i][j];
int ans=1e9;
if(a[i]==a[j])
ans=min(ans,dfs(i+,j-));
for(int k=i;k<j;k++)
ans=min(ans,dfs(i,k)+dfs(k+,j));
return dp[i][j]=ans;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("%d\n",dfs(,n-));
return ;
}
CodeForces - 607B (记忆化搜索)的更多相关文章
- 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 ...
- CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化
Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...
- Codeforces Gym 100231G Voracious Steve 记忆化搜索
Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
- Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索
E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...
- CodeForces 398B 概率DP 记忆化搜索
题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...
- Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...
- Codeforces 148D Bag of mice:概率dp 记忆化搜索
题目链接:http://codeforces.com/problemset/problem/148/D 题意: 一个袋子中有w只白老鼠,b只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...
- codeforces 284 D. Cow Program(记忆化搜索)
题目链接:http://codeforces.com/contest/284/problem/D 题意:给出n个数,奇数次操作x,y都加上a[x],偶数次操作y加上a[x],x减去a[x],走出了范围 ...
- codeforces 793 D. Presents in Bankopolis(记忆化搜索)
题目链接:http://codeforces.com/contest/793/problem/D 题意:给出n个点m条边选择k个点,要求k个点是联通的而且不成环,而且选的边不能包含选过的边不能包含以前 ...
随机推荐
- zookeeper【2】集群管理
Zookeeper 的核心是广播,这个机制保证了各个Server之间的同步.实现这个机制的协议叫做Zab协议. Zab协议有两种模式,它们分别是恢复模式(选主)和广播 模式(同步).当服务启动或者在领 ...
- Java 8 读取文件
以前的Java版本中读取文件非常繁琐,现在比较简单.使用Java8的Files以及Lambda,几句代码就可以搞定. public static String getXml() { StringBuf ...
- svn 未提交的显示黑色的星*
1.在eclipse中,选择window-->Preferences,里面找到svn,如下图,勾选上Outgoing changes即可
- 第11章 Media Queries 与Responsive 设计
Media Queries--媒体类型(一) 随着科学技术不断的向前发展,网页的浏览终端越来越多样化,用户可以通过:宽屏电视.台式电脑.笔记本电脑.平板电脑和智能手机来访问你的网站.尽管你无法保证一个 ...
- BZOJ3992: [SDOI2015]序列统计(NTT 原根 生成函数)
题意 题目链接 给出大小为\(S\)的集合,从中选出\(N\)个数,满足他们的乘积\(\% M = X\)的方案数 Sol 神仙题Orz 首先不难列出最裸的dp方程,设\(f[i][j]\)表示选了\ ...
- 关于开发Cesium造成的电脑风扇狂飙的问题
最近在开发Cesium的项目,每次一打开浏览器渲染3D 模型.风扇就狂飙起来,进任务管理器查看发现集显使用率100%,独显使用率0%.使用的是集显进行渲染.怪不得风扇会飙起来.既然知道问题所在,解决的 ...
- J2EE项目工具集(转)
1.支持重构,TDD, Debug J2EE应用和Flying Error提示的IDE a.重构:即使团队用的最多的只是Rename,Move,Extract Method等有限几个最基本的功能,但J ...
- 封装一个MPermissionsActivity的思路和步骤
http://blog.csdn.net/longkehuawei/article/details/53202804 第一步:检测所有的权限是否都已授权 /** * 检测所有的权限是否都已授权 * * ...
- Nginx 性能参数优化
user www www; # ginx要开启的进程数 一般等于cpu的总核数,没必要开那么多,1个nginx内存消耗10兆左右 worker_processes 4; # 为每个进程分配cpu,上例 ...
- Xwiki安装部署详解
一.Xwiki简介XWiki是一个由Java编写的基于LGPL协议发布的开源wiki和应用平台.XWiki是一款基于java所编写的wiki,它可以运行在如Tomcat,Jetty,JBoss,Web ...