题目链接:

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的更多相关文章

  1. hdu 5693 && LightOj 1422 区间DP

    hdu 5693 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5693 等差数列当划分细了后只用比较2个或者3个数就可以了,因为大于3的数都可以由2和3 ...

  2. HDU 5115 Dire Wolf 区间dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5115 Dire Wolf Time Limit: 5000/5000 MS (Java/Others ...

  3. hdu 4597 Play Game 区间dp

    Play Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=459 ...

  4. hdu 4745 Two Rabbits 区间DP

    http://acm.hdu.edu.cn/showproblem.php?pid=4745 题意: 有两只兔子Tom Jerry, 他们在一个用石头围城的环形的路上跳, Tom只能顺时针跳,Jerr ...

  5. hdu 5181 numbers——思路+区间DP

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5181 题解:https://www.cnblogs.com/Miracevin/p/10960717.ht ...

  6. HDU 1141---Brackets Sequence(区间DP)

    题目链接 http://poj.org/problem?id=1141 Description Let us define a regular brackets sequence in the fol ...

  7. HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

    题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...

  8. hdu 2476(第一道区间dp)

    题意:就是给定两个字符串,第一个是初始串,第二个是目标串,问你把初始串变到目标串最少需要多少串! 分析:此题分两步,第一步是假设开始的初始串是空串,然后就进行区间dp,dp[i][j]代表把区间[i, ...

  9. HDU 4632 Palindrome subsequence (区间DP)

    题意 给定一个字符串,问有多少个回文子串(两个子串可以一样). 思路 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字符串中[i,j]位置中出现的回文子序 ...

随机推荐

  1. POJ C程序设计进阶 编程题#5:细菌分组实验

    编程题#5:细菌实验分组 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 有 ...

  2. CentOS 6 lnmp环境脚本

    实验环境:CentOS 6.3 32位 首先我们先去下载nginx的第三方yum源 mkdir /shell cd /shell wget http://www.atomicorp.com/insta ...

  3. pm2 开机自启动如何弄?

    1.使用pm2启动node :# pm2 start /home/wwwroot/web.js --watch 2.dump这些进程列表:# pm2 save 3.生成自启动脚本:# pm2 star ...

  4. 关于PYTHON_EGG_CACHE无权限的问题

    Perhaps your account does not have write access to this directory? You can change the cache director ...

  5. scala学习资料

    强烈推荐一个s在线学习scala的网站: http://zh.scala-tour.com/#/overview

  6. Ruby判断文件是否存在

    flag = FileTest::exist?("LochNessMonster") flag = FileTest::exists?("UFO") # exi ...

  7. C#模糊查询绑定datagridview

    private CollectionViewSource wgdData = new CollectionViewSource(); private DataTable Ds_wgd { get { ...

  8. android开发系列之git常用命令

    最近因为跳槽到新公司,然后新公司里面的代码管理工具是gitLab,所以我想在这篇博客里面整理一下git常用的语法. GitLab是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托 ...

  9. C#操作FTP, FTPHelper和SFTPHelper

    1. FTPHelper using System; using System.Collections.Generic; using System.IO; using System.Net; usin ...

  10. RCF

    1. RCF: 纯c++的RPC, 不引入IDL, 大量用到boost,比较强大.2. casocklib:  protobuf + asio 较完善实现3. eventrpc: protobuf + ...