CodeForces 607B zuma
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.
题解:题解:区间DP。若区间i~j为回文串,那么i+1~j-1也必然为回文串,dp[i][j]=dp[i+1][j-1],
如果不是,这就枚举中间点,区间DP裸题;
参考代码为:
#include<bits/stdc++.h>
using namespace std; int num[], dp[][];
int main()
{
int n;
while (~scanf("%d", &n))
{
memset(num, , sizeof(num));
memset(dp,0x3f,sizeof(dp));
for (int i = ; i <= n; i++)
scanf("%d", &num[i]);
for (int i = ; i <= n; i++)
dp[i][i] = ;
for (int i = n; i >= ; i--)
for (int j = i + ; j <= n; j++)
{
if (num[i] == num[j])
{
if (abs(j - i) != )
dp[i][j] = dp[i + ][j - ];
else
dp[i][j] = ;
} for (int k = i; k<j; k++)
{
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + ][j]);
}
}
printf("%d\n", dp[][n]);
}
return ;
}
CodeForces 607B zuma的更多相关文章
- Codeforces 607B Zuma(区间DP)
题目大概说,有n个颜色的宝石,可以消除是回文串的连续颜色序列,问最少要几下才能全部消除. 自然想到dp[i][j]表示序列i...j全部消除的最少操作数 有几种消除的方式都能通过枚举k(i<=k ...
- codeforces 607B. Zuma 区间dp
题目链接 给一个长度为n的序列, 每一次可以消去其中的一个回文串, 问最少几次才可以消完. 代码很清楚 #include <iostream> #include <vector> ...
- Zuma CodeForces - 607B (区间DP)
大意: 给定字符串, 每次删除一个回文子串, 求最少多少次删完. #include <iostream> #include <cstdio> #define REP(i,a,n ...
- CodeForces - 607B (记忆化搜索)
传送门: http://codeforces.com/problemset/problem/607/B Genos recently installed the game Zuma on his ph ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
- Codeforces Round #336 (Div. 2) D. Zuma
Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...
- 【44.19%】【codeforces 608D】Zuma
time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- Codeforces Round #336 Zuma
D. Zuma time limit per test: 2 seconds memory limit per test: 512 megabytes input: standard input ...
- 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 ...
随机推荐
- idea 常用功能
Ctrl + E:打开最近文件 双击 Shift:按文件名查找文件 Ctrl + Shift + F:全局搜索 Alt + ~(数字 1 左边的键):commit.push 代码 ...
- (Codeforce)Correct Solution?
One cold winter evening Alice and her older brother Bob was sitting at home near the fireplace and g ...
- 详细讲解 Redis 的两种安装部署方式
Redis 是一款比较常用的 NoSQL 数据库,我们通常使用 Redis 来做缓存,这是一篇关于 Redis 安装的文章,所以不会涉及到 Redis 的高级特性和使用场景,Redis 能够兼容绝大部 ...
- javascript 使用 setInterval 实现倒计时
javascript 使用 setInterval 实现倒计时 var timer = setInterval(function () { console.log(valid_time); if (v ...
- Python 编程语言要掌握的技能之一:善用变量来改善代码质量
如何为变量起名 在计算机科学领域,有一句著名的格言(俏皮话): There are only two hard things in Computer Science: cache invalidati ...
- 新闻实时分析系统-Kafka分布式集群部署
Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.Spa ...
- 2019/12/1 智能硬件实验室(ROS方向)
浅谈安装ubuntu与ros感想 ubuntu 以前看电脑软件安装管家上的教程尝试在另一台电脑上安装ubuntu(虚拟机上),但是后面遇到了无法解决的问题,放弃了.这次因为选的ros方向,所以昨天在学 ...
- 题解——面积(area.cpp)
题目来源&题面简述: 思路与算法选择: 只有*里面的部分对我们有用,所以可以将 *号外的部分标记一下. 可以用著名的BFS大法实现此过程.(连通块) 连通块模板: #include<bi ...
- JAVA合并多个word文档根据文章标题生成目录
此产品版本是免费版的,我也是在用免费,除了只能单次识别25张一下的word和生成pdf有限制,其他的功能都和正式版差不多. 如果你几十个文档,每个文档几页,输出出来超过25页,那没关系,依然可以使用. ...
- nginx集群架构
Linux集群从功能分类 高可用集群,高可用集群通常为俩台服务器,一台工作,另一台冗余,当提供服务器的服务器宕机时候,冗余服务器将接替宕机的服务器继续提供服务.实现高可用的集群开源软件有Heatbea ...