传送门

Description

给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-40),问最大能合出多少。注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3。

Input

输入的第一行是一个数字n,代表地图大小。然后n行,i+1行代表第i个数的大小

Output

输出仅一行,为最大能合并出的大小

Hint

1<=n<=248,不保证所有的数字能被合成完

Sample Input


Sample Output


solution

典型的区间DP。首先考虑区间dp最普通状态定义:用f[i][j]表示区间[i,j]所能合成的最大答案,转移为f[i][j]=max{f[i][k]+1|当且仅当f[i][k]==f[k+1][j]}。输出f[1][n],这么做本题存在缺陷:在转移时,无法保证区间f[i][j]的最大值在所需要的左(右)端点处被取到。例如:观察线段

此时f[1][3]=5,f[4][6]=5,但是无法进行合并,因为[1,3]等于5的位置在左侧,与[4,6]并不直接相连。

考虑增加维度,当前转移为n^3,增加维度时间爆炸,否定

考虑限制状态,记f[i][j]为区间[i,j]恰好能够合成的最大值,每次转移维护ans。转移同上。可行

  对于区间[i,j]的最大值一定可以由其两个子区间的最优解转移得到。

  证明:手推可能hack的情况,发现显然。

Code

#include<cstdio>
#define maxn 250 inline void qr(int &x) {
char ch=getchar();int f=;
while(ch>''||ch<'') {
if(ch=='-') f=-;
ch=getchar();
}
while(ch>=''&&ch<='') x=(x<<)+(x<<)+(ch^),ch=getchar();
x*=f;
return;
} inline int max(int a,int b) {return a>b?a:b;}
inline int min(int a,int b) {return a<b?a:b;} inline void swap(int &a,int &b) {
int c=a;a=b;b=c;return;
} int n,num[maxn],frog[maxn][maxn],ans; int main() {
qr(n);
for(int i=;i<=n;++i) {
qr(num[i]);frog[i][i]=num[i];
}
for(int i=;i<n;++i) {
for(int j=;j<=n;++j) {
int r=j+i;if(r>n) break;
for(int k=j;k<r;++k) {
if(!(frog[j][k]^frog[k+][r])) frog[j][r]=max(frog[j][r],frog[j][k]+),ans=max(ans,frog[j][r]);
}
}
}
printf("%d\n",ans);
return ;
}

summary

  对于DP时发现状态不够严谨导致转移困难,可以考虑对状态加以限制。在每次转移时更新答案。

  另外据说这题有大常数O(n)算法。可惜我不会

End on 2018/6/3

【区间DP】【lgP3146】248的更多相关文章

  1. 又一道区间DP的题 -- P3146 [USACO16OPEN]248

    https://www.luogu.org/problemnew/show/P3146 一道区间dp的题,以区间长度为阶段; 但由于要处理相邻的问题,就变得有点麻烦; 最开始想了一个我知道有漏洞的方程 ...

  2. 【bzoj4580】[Usaco2016 Open]248 区间dp

    题目描述 Bessie likes downloading games to play on her cell phone, even though she does find the small t ...

  3. 「USACO16OPEN」「LuoguP3146」248(区间dp

    题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small to ...

  4. 「区间DP」「洛谷PP3146 」[USACO16OPEN]248 G

    [USACO16OPEN]248 G 题目: 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...

  5. 一道另类的区间dp题 -- P3147 [USACO16OPEN]262144

    https://www.luogu.org/problemnew/show/P3147 此题与上一题完全一样,唯一不一样的就是数据范围; 上一题是248,而这一题是262144; 普通的区间dp表示状 ...

  6. 「USACO16OPEN」「LuoguP3147」262144(区间dp

    P3147 [USACO16OPEN]262144 题目描述 Bessie likes downloading games to play on her cell phone, even though ...

  7. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  8. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  9. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

随机推荐

  1. .net web api应用遇到的一些问题

    1.调用webapi接口时,碰到一种情况: 通过webapi调用接口时,返回的json数据,死活转换不成对象,转换的对象一直为null: webapi端代码: [HttpGet] public str ...

  2. Git 新建文件并提交

    1.创建一个readme.txt. cd /home/cyp/learngit touch readme.txt vim readme.txt 编写内容, wq 保存推出 2.提交步骤 2.1  gi ...

  3. 基于Python的接口自动化

    第一步 Python的安装配置 打开官网: https://www.python.org/downloads/ 目前官网上已经更新到3.6.1啦,有两个版本,大家可以按自己喜欢的去下载,我自己选择的是 ...

  4. OpenMPI运行问题:enough slots available in the system

    版本: Open MPI 3.0.1 编译好可执行的C语言程序后,使用 mpirun -np 3 Test 命令,发现没有正常运行,而是报错: There are not enough slots a ...

  5. 【Linux 运维】Centos7初始化网络配置

    设置网络 (1)动态获取一个IP地址 #dhclient        系统自动自动获取一个IP地址#ip addr         查看获取的ip地址(2)查看网关,子网掩码 虚拟机编辑>虚拟 ...

  6. LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

    1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...

  7. opencv-学习笔记(6)图像梯度Sobel以及canny边缘检测

    opencv-学习笔记(6)图像梯度Sobel以及canny边缘检测 这章讲了 sobel算子 scharr算子 Laplacion拉普拉斯算子 图像深度问题 Canny检测 图像梯度 sobel算子 ...

  8. POJ 2986 A Triangle and a Circle(三角形和圆形求交)

    Description Given one triangle and one circle in the plane. Your task is to calculate the common are ...

  9. Python字符串中的r前缀

    在Python中,如果字符串的前面有r/R前缀,那么,就会禁用转义符\的功能: >>>path = r'C:\new\text.dat' >>>pah 'C:\\n ...

  10. ajax的一些实用技巧

    1.尽量优先采用ajax获取html文件,然后再操作dom把数据填充到里面 在实际项目中,如果前端开发人员没有把页面给切分开,那么有如下两种办法可供选择:其一是,在各种点击事件中,用js去拼接并在拼接 ...