codeforces 607B. Zuma 区间dp
给一个长度为n的序列, 每一次可以消去其中的一个回文串, 问最少几次才可以消完。
代码很清楚
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int dp[][], a[];
int dfs(int l, int r) {
if(dp[l][r])
return dp[l][r];
dp[l][r] = inf;
if(l == r)
return dp[l][r] = ;
if(r-l == )
return dp[l][r] = (a[l]==a[r])?:;
if(a[l] == a[r])
dp[l][r] = dfs(l+, r-);
for(int i = l; i<=r; i++) {
dp[l][r] = min(dp[l][r], dfs(l, i)+dfs(i+, r));
}
return dp[l][r];
}
int main()
{
int n;
cin>>n;
for(int i = ; i<=n; i++) {
scanf("%d", &a[i]);
}
cout<<dfs(, n);
return ;
}
codeforces 607B. Zuma 区间dp的更多相关文章
- CodeForces 607B zuma
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the ...
- CF607B Zuma(区间dp)
题意 题目链接 Sol 裸的区间dp,转移的时候判一下两个字符是否相等即可 #include<bits/stdc++.h> #define Pair pair<int, int> ...
- Palindromic characteristics CodeForces - 835D (区间DP,预处理回文串问题)
Palindromic characteristics of string s with length |s| is a sequence of |s|integers, where k-th num ...
- BZOJ 1032 JSOI 2007 祖码Zuma 区间DP
题目大意:依照祖玛的玩法(任意选颜色),给出一段区间.问最少用多少个球可以把全部颜色块都消除. 思路:把输入数据依照连续的块处理.保存成颜色和数量.然后用这个来DP.我们知道,一个单独的块须要两个同样 ...
- CodeForces - 1114D-Flood Fill (区间dp)
You are given a line of nn colored squares in a row, numbered from 11 to nn from left to right. The ...
- Codeforces 607B Zuma(区间DP)
题目大概说,有n个颜色的宝石,可以消除是回文串的连续颜色序列,问最少要几下才能全部消除. 自然想到dp[i][j]表示序列i...j全部消除的最少操作数 有几种消除的方式都能通过枚举k(i<=k ...
- 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 ...
- 【CF607B】Zuma——区间dp(记忆化搜索/递推)
以下是从中文翻译成人话的题面: 给定一个长度小于等于500的序列,每个数字代表一个颜色,每次可以消掉一个回文串,问最多消几次可以消完? (7.16) 这个题从洛谷pend回来以后显示有103个测试点( ...
- Codeforces 1107E(区间dp)
用solve(l, r, prefix)代表区间l开始r结束.带了prefix个前缀str[l](即l前面的串化简完压缩成prefix-1个str[l],加上str[l]共有prefix个)的最大值. ...
随机推荐
- mongoose 查询子文档的方法
{ "__v": 1, "_id": "538f5f0f6195a184108c8bd8", "title": &quo ...
- NSFileHandle编写json数据格式
代码如下: + (void)writeToFile:(NSDictionary *)params filePath:(NSString *)path { NSData *jsonData = [sel ...
- gallery利用代码定位图片并且不丢失动画效果
安卓中,利用gallery.setSelection(position);可以手动定位图片 但是众所周知会丢失动画效果 即使是用gallery.setSelection(position,true); ...
- SPOJ QTREE4 lct
题目链接 这个题已经处于花式tle了,改版后的spoj更慢了.. tle的话就多交几把... #include <iostream> #include <fstream> #i ...
- Objective C 链式调用
起因 某日使用DKChainableAnimationKit的时候发现可以如下写代码: view.animation.rotate(180).anchorTopLeft.thenAfter(1.0). ...
- JavaScript引用类型之Array数组之强大的splice()方法
splice()方法可以说是Array数组最强大的方法,他的用法很多,主要用法是向数组的中部插入项! 下面是它的用法: arrayObject.splice(index,howmany,element ...
- sqlserver字符串拆分(split)方法汇总
--方法0:动态SQL法declare @s varchar(100),@sql varchar(1000)set @s='1,2,3,4,5,6,7,8,9,10'set @sql='select ...
- python学习之 dictionary 、list、tuple操作
python 内置类型数据 有dictionary(字典).list(列表)和tuple(元组) 一.Dictionary Dictionary 是 Python 的内置数据类型之一,它定义了键和值之 ...
- cocos2d-x学习日志(12) --弹出对话框的设计与实现
我们时常需要这么些功能,弹出一个层,给与用户一些提示,这也是一种模态窗口,在没有对当前对话框进行确认的时候,不能继续往下操作. 功能分析 我们设计一个对话框,对话框上有几个按钮(个数可定制),当然有个 ...
- for循环例子1、2、3
/* Name:for循环例子1.2.3 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月12日 02:12:41 Description:该代码用来熟悉 ...