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个)的最大值. ...
随机推荐
- Android自定义Activity酷炫的动画跳转效果
两个Activity跳转的时候,自定义翻页效果: Intent intent = new Intent(FirstActivity.this, SecondActivity.class); sta ...
- 不定高度实现垂直居中(兼容低版本ie)
css实现垂直居中的方法比较多,但是每种方法的缺陷也很明显,我尝试对其中一种方法进行了改良 先看原方法: <div class="parent"> <div cl ...
- Server.MapPath(string sFilePath) 报未将对象引用到实例异常
System.Web.HttpContext.Current.Server.MapPath(string sfilePath)将虚拟路径转换成物理路径.这个必须在aspx或者MVC中Action调用才 ...
- 创建Java项目报错处理
好久没用Eclipse编写Java程序了,今天创建一个Java项目的时候,老报错,错误信息如下: Implicit super constructor Object() is undefined fo ...
- VS2015使用scanf报错解决方案
版权声明:本文为博主原创文章,未经博主允许不得转载. 方法一:在程序最前面加#define _CRT_SECURE_NO_DEPRECATE: 方法二:在程序最前面加#define _CRT_SECU ...
- c# session总结
C# 中对 Session 的“(string)”.“.ToString()”与“Convert.ToString”用法笔记 在实际操作当中,我们经常会遇到将 Session 的值转为 String ...
- OC 中的block使用
在iOS的开发过程中,使用块的地方很多也很方便,但是在使用块的过程中要注意内存泄露的问题. 在块创建的时候,会对块内的所有对象的引用计数加一,直到块销毁,所以在使用块的过程中需要我们进行处理,在这里以 ...
- apache+mysql+php环境的手动搭建
一.搭建Apache Http Server 官方下载地址:http://www.apachehaus.com/cgi-bin/download.plx 搭建环境:win10 64位 WIN10 64 ...
- [非技术参考]C#基础:使用Thread创建线程(1)
Thread类可以创建和控制线程,Thread类的构造函数重载为接受ThreadStart和ParameterizedThreadStart类型的委托参数.下面我们用一个例子来解释怎样用Thread类 ...
- h5的api dom全屏展示
下面是完整的例子,暂不做分析 <!DOCTYPE html> <html> <head> <title> FullScreen API 演示</t ...