HDU 5693 D Game 区间dp
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5693
题解:
一种朴实的想法是枚举选择可以删除的两个或三个数(其他的大于三的数都能凑成2和3的和),删掉。然后一直递归下去。但删除子串的操作不容易,而且搜索复杂度有点大,记忆化判相同子序列也不容易。
可以看出,这些问题都是由于删除这个操作引起的。
因此为了保证dp的可操作性,我们没必要真的删除,另dp[l][r]表示区间[l,r]能删除的最大个数,如果dp[l][r]==r-l+1说明这一段是都可以删除的。这样子就可以用区间dp乱搞做出来了。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; const int maxn = ; int n, m;
int arr[maxn], mat[maxn][maxn],dp[maxn][maxn]; void init() {
memset(mat, , sizeof(mat));
memset(dp, , sizeof(dp));
} void solve() {
//dp[l][r]==r-l+1说明l和r之间是可以全部删除掉的。
for (int len = ; len <= n; len++) {
for (int l = ; l <= n; l++) {
int r = l + len; if (r > n) break;
dp[l][r] = dp[l + ][r - ];
if (mat[l][r]&&dp[l + ][r - ] == r - l - )
dp[l][r] = max(dp[l][r], dp[l + ][r - ] + ); for (int i = l + ; i < r; i++) {
if (mat[l][i] && dp[l + ][i - ] == i - l - )
dp[l][r] = max(dp[l][r], dp[l + ][i - ] + dp[i + ][r] + );
if (mat[i][r] && dp[i + ][r - ] == r - i - )
dp[l][r] = max(dp[l][r], dp[l][i - ] + dp[i + ][r - ] + );
if (mat[l][i] && mat[i][r] && arr[r] - arr[i] == arr[i] - arr[l] &&
dp[l + ][i - ] == i - l - && dp[i + ][r - ] == r - i - ) {
dp[l][r] = max(dp[l][r], r - l + );
}
}
//拆掉i,i+1的匹配。
for (int i = l; i <r; i++) {
dp[l][r] = max(dp[l][r], dp[l][i] + dp[i+][r]);
}
}
}
} int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
scanf("%d%d", &n, &m); init();
for (int i = ; i <= n; i++) scanf("%d", arr + i);
for (int i = ; i < m; i++) {
int d; scanf("%d", &d);
for (int i = ; i <= n; i++) {
for (int j = i + ; j <= n; j++) {
if (arr[j] - arr[i] == d) mat[i][j] = ;
}
}
}
solve(); printf("%d\n", dp[][n]);
}
return ;
}
HDU 5693 D Game 区间dp的更多相关文章
- hdu 5693 && LightOj 1422 区间DP
hdu 5693 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5693 等差数列当划分细了后只用比较2个或者3个数就可以了,因为大于3的数都可以由2和3 ...
- HDU 5115 Dire Wolf 区间dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5115 Dire Wolf Time Limit: 5000/5000 MS (Java/Others ...
- hdu 4597 Play Game 区间dp
Play Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=459 ...
- hdu 4745 Two Rabbits 区间DP
http://acm.hdu.edu.cn/showproblem.php?pid=4745 题意: 有两只兔子Tom Jerry, 他们在一个用石头围城的环形的路上跳, Tom只能顺时针跳,Jerr ...
- hdu 5181 numbers——思路+区间DP
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5181 题解:https://www.cnblogs.com/Miracevin/p/10960717.ht ...
- HDU 1141---Brackets Sequence(区间DP)
题目链接 http://poj.org/problem?id=1141 Description Let us define a regular brackets sequence in the fol ...
- HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...
- hdu 2476(第一道区间dp)
题意:就是给定两个字符串,第一个是初始串,第二个是目标串,问你把初始串变到目标串最少需要多少串! 分析:此题分两步,第一步是假设开始的初始串是空串,然后就进行区间dp,dp[i][j]代表把区间[i, ...
- HDU 4632 Palindrome subsequence (区间DP)
题意 给定一个字符串,问有多少个回文子串(两个子串可以一样). 思路 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字符串中[i,j]位置中出现的回文子序 ...
随机推荐
- spark-submit工具参数说明
执行时需要传入的参数说明 Usage: spark-submit [options] <app jar | python file> [app options] 参数名称 含义 --mas ...
- 一款jQuery实现重力弹动模拟效果特效,弹弹弹,弹走IE6
一款jQuery实现重力弹动模拟效果特效 鼠标经过两块黑色div中间的红色线时,下方的黑快会突然掉落, 并在掉落地上那一刻出现了弹跳的jquery特效效果,非常不错,还兼容所有的浏览器, 适用浏览器: ...
- discuz 模拟批量上传附件发帖
discuz 模拟批量上传附件发帖 简介 对于很多用discuz做资源下载站来说,一个个上传附件,发帖是很繁琐的过程.如果需要批量上传附件发帖,就需要去模拟discuz 上传附件的流程. 插件地址 h ...
- How to using to code import to GL journal[AX2012]
static void THK_importLedgerJournalTrans(Args _args) { Filename fileName = "C:\\Users\\ksiu3880 ...
- Objective-C关于分类、扮演、协议
-----<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培 ...
- 【FAQ】【JSP】HTTP Status 500 - Summary(问题排查时候应该仔细分析所有的错误打印说明)
Question 1.HTTP Status 500 - Unable to compile class for JSP:'***' cannot be resolved to a type 原因分析 ...
- SQLServer异常捕获
在SQLserver数据库中,如果有很多存储过程的时候,我们会使用动态SQL进行存储过程调用存储过程,这时候,很可能在某个环节就出错了,但是出错了我们很难去跟踪到出错的存储过程,此时我们就可以使用异常 ...
- Javascript中“==”与“===”的区别
在Javascript中有"=="和"==="两种比较运行符,那么他们有什么区别呢? 一.对于string,number等基础类型,==和===是有区别的 1) ...
- 正益工作是何许APP?凭什么作为第一届大会的“闪亮”点
参加过很多发布会,看过很多宣传稿,渐渐的你也读懂了“大会亮点”,这是技术人.市场人绞尽脑汁.加班加点的成果,更渗透着企业未来的战略思路.在2016AppCan移动开发者大会的官方新闻发布后,很多人留言 ...
- hdu 4150 Powerful Incantation
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4150 Powerful Incantation Description Some dangerous ...