Codeforces Round #336 (Div. 2) D. Zuma(区间DP)
题目链接:https://codeforces.com/contest/608/problem/D
题意:给出n个宝石的颜色ci,现在有一个操作,就是子串的颜色是回文串的区间可以通过一次操作消去,问最少需要多少次操作可以消除所有的宝石。(每次操作消除一个回文串,最少操作次数清楚字符串)
题解:dp[l][r]表示区间(l,r)的最少操作次数,对于一个区间(l,r),有两种转移:①若存在c_l = c_i(l <= i <= r),可以由dp[l][i] + dp[i + 1][r]转移;②若c_l = c_r,可以由dp[l + 1][r - 1]转移。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define mp(a,b) make_pair(a,b)
#define pi acos(-1)
#define pii pair<int,int>
#define pb push_back
#define lowbit(x) ((x)&(-x))
const int INF = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = + ;
const int maxm = 1e6 + ;
const ll mod = ; int c[maxn];
int dp[maxn][maxn]; int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int n;
scanf("%d",&n);
for(int i = ; i < n; i++) {
scanf("%d",&c[i]);
dp[i][i] = ;
}
for(int i = ; i < n - ; i++) {
if(c[i] == c[i + ]) dp[i][i + ] = ;
else dp[i][i + ] = ;
}
for(int i = ; i < n; i++) {
for(int j = ; i + j < n; j++) {
dp[j][j + i] = i + ;
for(int k = j; k < j + i; k++) {
if(c[j] == c[k]) dp[j][j + i] = min(dp[j][j + i], dp[j][k] + dp[k + ][j + i]);
}
if(c[j] == c[j + i]) dp[j][j + i] = min(dp[j][j + i], dp[j + ][j + i - ]);
}
}
printf("%d\n",dp[][n - ]);
return ;
}
Codeforces Round #336 (Div. 2) D. Zuma(区间DP)的更多相关文章
- 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 ...
- Codeforces Round #336 (Div. 2) D. Zuma
Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...
- Codeforces Round #336 (Div. 2)B 暴力 C dp D 区间dp
B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 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)C. Chain Reaction DP
C. Chain Reaction There are n beacons located at distinct positions on a number line. The i-th bea ...
- Codeforces Round #538 (Div. 2)D(区间DP,思维)
#include<bits/stdc++.h>using namespace std;int a[5007];int dp[5007][5007];int main(){ int n ...
- Codeforces Round #336 (Div. 2) 608C Chain Reaction(dp)
C. Chain Reaction time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】
A. Saitama Destroys Hotel time limit per test:1 second memory limit per test:256 megabytes input:sta ...
随机推荐
- 如何用C++实现栈
栈的定义 栈(stack)又名堆栈,它是一种运算受限的线性表.限定仅在表尾进行插入和删除操作的线性表.这一端被称为栈顶,相对地,把另一端称为栈底.向一个栈插入新元素又称作进栈.入栈或压栈,它是把新元素 ...
- 032 Android Service
1.介绍 2.新建Service (1) (2)在Androidmanifest.xml文件中配置service <service android:name=".Myservice&q ...
- WUSTOJ 1305: 最短路(Java)
题目链接:
- 数据结构-单链表-类定义2-C++
上一次的C++链表实现两个单链表的连接不太理想,此次听了一些视频课,自己补了个尾插法,很好的实现了两个链表的连接,当然了,我也是刚接触,可能是C++的一些语法还不太清楚,不过硬是花了一些时间尽量在数据 ...
- AS3.0 位图(BMP)解析类
/** * *-----------------------------* * | *** BMP格式解析类 *** | * *-----------------------------* * * 编 ...
- js new到底做了什么?如何重写new?(转)
转自:https://blog.csdn.net/lyt_angularjs/article/details/86623988
- 浅学CLR via C#笔记之类型转换
我们都知道CLR最重要的一个特性就是类型安全,它在运行时就知道对象类型. 但我们会经常用到将一种类型转换成另一种类型,CLR也允许将对象转成他的实际类型,或者是它的基类型. 在C#中,支持隐士转换成它 ...
- GOF学习笔记1:术语
1.abstract class 抽象类定义了一个接口,把部分或全部实现留给了子类,不能实例化. 2.abstract coupling 抽象耦合如果一个类A引用了另一个抽象类B,那么就说A是抽象耦 ...
- 哈夫曼树详解——PHP代码实现
在介绍哈夫曼树之前需要先了解一些专业术语 路径和路径长度 在一棵树中,从一个结点往下可以达到的孩子或孙子结点之间的通路,称为路径.通路中分支的数目称为路径长度.若规定根结点的层数为1,则从根结点到第L ...
- 【小知识点】js无需刷新在url地址添加参数
今天后端同事找我,问我一个关于js无需刷新在url地址添加参数的方法. 然后我百度啊,终于在一篇文章找到了办法,非常简单的一句代码. window.history.pushState({}, 0, w ...