【区间DP】【lgP3146】248
传送门
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的更多相关文章
- 又一道区间DP的题 -- P3146 [USACO16OPEN]248
https://www.luogu.org/problemnew/show/P3146 一道区间dp的题,以区间长度为阶段; 但由于要处理相邻的问题,就变得有点麻烦; 最开始想了一个我知道有漏洞的方程 ...
- 【bzoj4580】[Usaco2016 Open]248 区间dp
题目描述 Bessie likes downloading games to play on her cell phone, even though she does find the small t ...
- 「USACO16OPEN」「LuoguP3146」248(区间dp
题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small to ...
- 「区间DP」「洛谷PP3146 」[USACO16OPEN]248 G
[USACO16OPEN]248 G 题目: 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- 一道另类的区间dp题 -- P3147 [USACO16OPEN]262144
https://www.luogu.org/problemnew/show/P3147 此题与上一题完全一样,唯一不一样的就是数据范围; 上一题是248,而这一题是262144; 普通的区间dp表示状 ...
- 「USACO16OPEN」「LuoguP3147」262144(区间dp
P3147 [USACO16OPEN]262144 题目描述 Bessie likes downloading games to play on her cell phone, even though ...
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
随机推荐
- NGUI制作流光效果
效果展示: 技巧: 1.勾选UIPanel下的Normal启用UI的法线贴图,并建立带有法线贴图的UI对象(此处用NGUI自带的Reflector.Atlas中的图作为UI). 2.建立点光源并为其添 ...
- Oracle启动与关闭数据库实例
Oracle数据库启动实例分为3个步骤: 启动实例 加载数据库 打开数据库 通用模式: STARTUP [ nomount | mount | open | force ] [resetrict] ...
- [JSON].connectionValue()
语法: [JSON].connectionValue() 说明: 将对象的所有键值接连成新的字符串值 返回: [String] 示例: Set a = toJson() c = Array(1,2,3 ...
- 关于java中“使用了未经检查或不安全的操作、有关详细信息,请使用 ——Xlint:unchecked重新编译”
今天看<算法 第4版>排序章节时,发现了一个了一个小问题.先贴一下代码: public class Selection{ public static void main(String[]a ...
- 微信小程序navigator跳转失效
在编写小程序时遇到一个问题:使用 <navigator url='/pages/lists/index'>...</navigator>进行跳转没有反应.控制台也没有报错,ap ...
- New Year and Old Property :dfs
题目描述: Limak is a little polar bear. He has recently learnt about the binary system. He noticed that ...
- 【转载】android 常用开源框架
对于Android初学者以及对于我们菜鸟,这些大神们开发的轻量级框架非常有用(更别说开源的了). 下面转载这10个框架的介绍:(按顺序来吧没有什么排名). 一. Afinal 官方介绍: Afina ...
- 十面阿里,七面头条,六个Offer,春招结束
作者:jkgeekjack链接:https://www.nowcoder.com/discuss/80156?type=0&order=0&pos=13&page=2来源:牛客 ...
- POJ 3028 Shoot-out(概率DP)
Description This is back in the Wild West where everybody is fighting everybody. In particular, ther ...
- Activity生命周期 与 Activity 之间的通信
一. Activity生命周期 上图 1. Activity状态 激活状态 : Activity出于前台 , 栈顶位置; 暂停状态 : 失去了焦点 , 但是用户仍然可以看到 , 比如弹出一个对话框 , ...