【44.19%】【codeforces 608D】Zuma
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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
3
1 2 1
output
1
input
3
1 2 3
output
3
input
7
1 4 4 2 3 2 1
output
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.
【题目链接】:http://codeforces.com/contest/608/problem/D
【题解】
记忆化搜索;
jyh(l,r);表示把l..r这个区间范围的数字全部去掉最少需要的操作次数;
如果a[l]==a[r];则整个问题转化为为jyh(l+1,r-1);
当然如果a[l]!=a[r]则还没完;
需要枚举一下分割点i;
把整个问题转化为l..i,和i+1..r两个部分;
如果i==l,则说明最左边那个单独一个取出来操作一次(自己单独一个肯定是回文);
如果l==r返回1;
如果l==r-1->(且a[l]==a[r]返回1,如果a[l]!=a[r]返回2;)
掌握一下那个分割序列的方法;
其他的则没有什么了;
最多就500*500个状态;
【完整代码】
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
using namespace std;
const int MAXN = 510;
const int INF = 0x3f3f3f3f;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
int n,a[MAXN],f[MAXN][MAXN];
void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
int jyh(int l,int r)
{
if (f[l][r]!=INF)
return f[l][r];
int *k = &f[l][r];
if (l == r)
return f[l][r] = 1;
if (l==r-1)
{
if (a[l]==a[r])
return f[l][r]=1;
else
return f[l][r] = 2;
}
if (a[l] == a[r])
*k = jyh(l+1,r-1);
for (int i = l;i <= r;i++)
*k = min(*k,jyh(l,i)+jyh(i+1,r));
return f[l][r];
}
int main()
{
// freopen("F:\\rush.txt","r",stdin);
memset(f,INF,sizeof(f));
rei(n);
for (int i = 1;i <= n;i++)
rei(a[i]);
printf("%d\n",jyh(1,n));
return 0;
}
【44.19%】【codeforces 608D】Zuma的更多相关文章
- 【44.19%】【codeforces 727C】Guess the Array
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【19.46%】【codeforces 551B】ZgukistringZ
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【44.64%】【codeforces 743C】Vladik and fractions
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【77.78%】【codeforces 625C】K-special Tables
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【30.43%】【codeforces 746C】Tram
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
随机推荐
- 学习 Perl(一) —— 安装及 hello world
所谓存在的即是合理的,尤其适用于琳琅满目的编程语言界.每种编程语言在设计之初均只为解决特定领域的特定问题而生,没有语言擅长所有的领域能够完美地解决所有的问题. 这里推荐一本经典的 perl 入门书:P ...
- socket长连接的维持
长连接的维持,是要客户端程序,定时向服务端程序,发送一个维持连接包的.如果,长时间未发送维持连接包,服务端程序将断开连接. 客户端:通过持有Client对象,可以随时(使用sendObject方法)发 ...
- iOS_06_Mac os X
Mac os X 系统简介 * 苹果公司专门为苹果电脑设计的操作系统. * 以坚如磐石的UNIX为基础,既简单易用且功能强大. * x 是一个罗马数字正式的发音位“十”(ten),连续了先前的Mac ...
- 【hdu 2328】Corporate Identity
[链接]h在这里写链接 [题意] 找一个字典序最小的公共最长子串; [题解] 后缀数组. 把所有的串用不同的分隔符分开.(大于'z'的分隔符); 然后求出那几个固定的数组. 二分一下那个子串的长度. ...
- Java Timer TimerTask Example(java Timer的例子)
Java java.util.Timer is a utility class that can be used to schedule a thread to be executed at cert ...
- jQuery笔记---选择器(二)
1.选择器练习: 1)查找UL中的元素的内容 格式:$(“ul li:XX”).text() XX:代表方法 比如:获取到第一元素,然后获取当中的值 $(“ul li:first”).text() 获 ...
- 10.13 android输入系统_多点触摸驱动理论与框架
1.多点触摸驱动理论 驱动程序仅上报多个触点的位置就可以,是放大还是缩小由应用程序控制 对于多点触摸驱动在linux系统中有个输入子系统,其已经实现了open/read/write等接口 我们只需要实 ...
- Topological Spaces(拓扑空间)
拓扑空间的定义有多种形式,通过 open sets(开集)的形式定义是最为常见的拓扑空间定义形式. 1. 通过开集(open sets)定义 拓扑空间由一个有序对 (X,τ) 表示,X 表示非空集合, ...
- php实现调整数组顺序使奇数位于偶数前面
php实现调整数组顺序使奇数位于偶数前面 一.总结 1.array_push()两个参数,$arr在前 2.array_merge()的返回值是数组 二.php实现调整数组顺序使奇数位于偶数前面 ...
- js实现操作等待提示loading……
js实现操作等待功能,防止重复提交,界面友好,底部为灰色遮罩层,防止用户重复操作. 先看效果图: 接着看js代码: //关闭等待窗口 function closeWaiting() { var b ...