CodeForces-607B:Zuma (基础区间DP)
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.
Example
3
1 2 1
1
3
1 2 3
3
7
1 4 4 2 3 2 1
2
题意:给定一排数字串,每次操作可以删去其中一段回文串,问至少需要多少次操作。
思路:区间DP,对长度为1和2的特殊处理:
如果为1,那么dp[i][i]=1;如果为2。
如果为2,那么dp[i][j]取决于a[i]和a[j]是否相同。
否则,先根据边界是否相同初始化,然后区间DP。
当然,也可以用普通DP实现,dp[i]=max(dpx),可以用hash验证后面是否是回文串。(感觉没问题吧。)
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const int inf=;
int dp[maxn][maxn],a[maxn];
int main()
{
int N,i,j,k;
scanf("%d",&N);
for(i=;i<=N;i++) scanf("%d",&a[i]);
for(i=N;i>=;i--){
for(j=i;j<=N;j++){
if(j-i==) dp[i][j]=;
else if(j-i==) dp[i][j]=(a[i]==a[j])?:;
else {
if(a[i]==a[j]) dp[i][j]=dp[i+][j-];
else dp[i][j]=dp[i+][j-]+;
for(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 (基础区间DP)的更多相关文章
- Codeforces 607B Zuma(区间DP)
题目大概说,有n个颜色的宝石,可以消除是回文串的连续颜色序列,问最少要几下才能全部消除. 自然想到dp[i][j]表示序列i...j全部消除的最少操作数 有几种消除的方式都能通过枚举k(i<=k ...
- Codeforces - 149D 不错的区间DP
题意:有一个字符串 s. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情 ...
- CodeForces 607B zuma
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the ...
- Codeforces.392E.Deleting Substrings(区间DP)
题目链接 \(Description\) \(Solution\) 合法的子序列只有三种情况:递增,递减,前半部分递增然后一直递减(下去了就不会再上去了)(当然还要都满足\(|a_{i+1}-a_i| ...
- Codeforces 983B. XOR-pyramid【区间DP】
LINK 定义了一种函数f 对于一个数组b 当长度是1的时候是本身 否则是用一个新的数组(长度是原数组-1)来记录相邻数的异或,对这个数组求函数f 大概是这样的: \(f(b[1]⊕b[2],b[2] ...
- CodeForces - 1025D: Recovering BST (区间DP)
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees! ...
- Codeforces 1114D Flood Fill (区间DP or 最长公共子序列)
题意:给你n个颜色块,颜色相同并且相邻的颜色块是互相连通的(连通块).你可以改变其中的某个颜色块的颜色,不过每次改变会把它所在的连通块的颜色也改变,问最少需要多少次操作,使得n个颜色块的颜色相同. 例 ...
- Codeforces 958C3 - Encryption (hard) 区间dp+抽屉原理
转自:http://www.cnblogs.com/widsom/p/8863005.html 题目大意: 比起Encryption 中级版,把n的范围扩大到 500000,k,p范围都在100以内, ...
- codeforces 607B. Zuma 区间dp
题目链接 给一个长度为n的序列, 每一次可以消去其中的一个回文串, 问最少几次才可以消完. 代码很清楚 #include <iostream> #include <vector> ...
随机推荐
- Day 9 Linux samba & ngnix
(摘) Samba服务 一.Samba简介 Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服 ...
- 3.JAVA语言基础部分—Class类与反射
什么是Java反射机制? JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的以及动态调用对象的方法的功能称为 ...
- Java教程收集
极客学院Wiki离线教程-Java类: 官网:http://wiki.jikexueyuan.com/list/java/ 离线版本:链接:http://pan.baidu.com/s/1pKD2oH ...
- 最新的hustoj搭建姿势
试着照某度上的教程搭了一下hustoj,出了一些问题,之前的搭建姿势很多已经不适用了,重新整理一下思路,方法二简单粗暴: 方法一: 首先虚拟机安装了Elementory OS (基于Ubuntu的衍生 ...
- PS 如何把大嘴变小嘴
Photoshop整容教程:让MM美唇大嘴变小嘴 2009-06-17 14:15作者:佚名出处:天极网软件频道责任编辑:王健 下面就开始实际操作了. 1.首先从Photosh ...
- Adobe Flash builder破解方法
Flash Builder 4 有许多新的特性,可以结合新的功能使用新的Flex 4框架创建出更炫的应用.基于用户的反馈,对以数据中心的开发也进行了优化:对类如配置从服务器返回的数据类型这样的任务,也 ...
- HBase技术简介
一.HBase简介 HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. ...
- 从Nginx源代码谈大写和小写字符转化的最高效代码以及ASCII码表的科学
说起大写和小写字母转换.大家非常easy想起系统函数是不是,差点儿全部的编程语言都提供了这样的转换函数,可是你有没有想过这背后是怎么实现的? 让你写怎么实现? 我们都知道Nginx是眼下用的最多的Ht ...
- SQL server 子查询 链接查询
数据库 if while else 的使用 数据库运算符优先级
- Codeforces 486E LIS of Sequence(线段树+LIS)
题目链接:Codeforces 486E LIS of Sequence 题目大意:给定一个数组.如今要确定每一个位置上的数属于哪一种类型. 解题思路:先求出每一个位置选的情况下的最长LIS,由于開始 ...