又一道区间DP的题 -- P3146 [USACO16OPEN]248
https://www.luogu.org/problemnew/show/P3146
一道区间dp的题,以区间长度为阶段;
但由于要处理相邻的问题,就变得有点麻烦;
最开始想了一个我知道有漏洞的方程
if(f[i][k] != f[k + ][j]) f[i][j] = max(f[i][k],f[k + ][j]);
else f[i][j] = max(f[i][j],f[i][k] + );
可能f[i][k] = f[i][j],但他们可合并的并未相邻;
可以这样
#include <bits/stdc++.h>
#define read read()
#define up(i,l,r) for(int i = (l);i <=(r); i++)
using namespace std;
int read
{
int x = ; char ch = getchar();
while(ch < || ch > ) ch = getchar();
while(ch >= && ch <= ) {x = * x + ch - ; ch = getchar();}
return x;
}
const int N = ;
int n,a[N],f[N][N],ans;
int main()
{
freopen("248.in","r",stdin);
n = read;
up(i,,n) a[i] = read;
up(i,,n) {f[i][i] = a[i];ans = max(ans,f[i][i]);printf("[%d,%d] : %d\n",i,i,f[i][i]);}
up(L,,n)
up(i,,(n - L + ))
{
int j = i + L - ;
f[i][j] = ;
up(k,i,j - )
{
//if(f[i][k] != f[k + 1][j])
//f[i][j] = max(f[i][k],f[k + 1][j]);
//else
if(f[i][k] == f[k + ][j])
f[i][j] = max(f[i][j],f[i][k] + );
}
ans = max(f[i][j],ans);
//printf("[%d,%d] : %d\n",i,j,f[i][j]);
}
printf("%d",ans);
//printf("%d",f[1][n]);
return ;
}
区间长度由小到大,并且只合并相邻的,更新答案;
保证了能输出最大值;
又一道区间DP的题 -- P3146 [USACO16OPEN]248的更多相关文章
- 状态压缩---区间dp第一题
标签: ACM 题目 Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is ...
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- 洛谷P3146 [USACO16OPEN]248
P3146 [USACO16OPEN]248 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- 洛谷 P3146 [USACO16OPEN]248
P3146 [USACO16OPEN]248 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- P3146 [USACO16OPEN]248
P3146 [USACO16OPEN]248 题解 第一道自己码出的区间DP快庆祝一哈 2048 每次可以合并任意相邻的两个数字,得到的不是翻倍而是+1 dp[L][R] 区间 L~R 合并结果 然后 ...
- 再一道区间DP -- P4170 [CQOI2007]涂色
https://www.luogu.org/problemnew/show/P4170 一道简单的区间DP,注意读入 #include <bits/stdc++.h> #define up ...
- poj 2955 区间dp入门题
第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i ...
- 二叉树问题(区间DP好题)
二叉树问题 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Petya Bulochkin很幸运:他得到了一份在"Macrohard"公司的工作.他想要展现他的才华, ...
- codeforces 1140D(区间dp/思维题)
D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
随机推荐
- eCharts 折线图,动态绑定数据不更新图表的问题,
官方文档 : http://echarts.baidu.com/tutorial.html npm install echarts --save let lineChart = echarts.ini ...
- JAVA高精度模板
刚开始还坚持用C++写高精来着,后来发现JAVA写高精方便太多了,所以也来学习一下JAVA高精度的模板. 参考:https://www.cnblogs.com/imzscilovecode/p/883 ...
- TZOJ 2725 See you~(二维树状数组单点更新区间查询)
描述 Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algo ...
- To be a better me
2017.4.15 突然成为前端实习生.做了两个周的官网项目,主要是更新官网一些文字图片以及新加一个页面,因为是静态页面,所以熟悉了代码就上手了.幸好没出什么差错. 两周后,实习结束. 2017.7. ...
- Django 改变xadmin后台英文为中文
1.标题 setting.py文件: LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' 修改: LANGUAGE_CODE = 'zh-Hans' TIME_ZONE ...
- linux操作系统重启后 解决nginx的pid消失问题
重启了linux服务器之后,进程性的 nginx -s stop后再次启动nginx -s reload ,总是会报错误nginx: [error] open() "/alidata/ser ...
- vue-router2
六,导航钩子 导航钩子函数主要是在导航跳转的时候做一些操作,比如跳转页面之前,进行判断 进而选择跳转到哪里 钩子函数根据生效范围根据其生效范围可以分为全局钩子函数,路由独享钩子函数 和 组件钩子函数. ...
- pyinstller的安装
下载:http://www.pyinstaller.org/ 解压到目录 切换到python目录 执行命令: python.exe D:\Download\PyInstaller-2.1\setup. ...
- jq以固定开头的class属性的名称
$("div[class^='pick']").css({'border-color':'#000000'}); div [class^="aaa"]
- linux操作系统-设置静态ip
在使用linux虚拟机的时候因为经常有关机的需求,然后重新开机后可能面临这上一次获取的ip被改变,在这里我分享一下在linux 下设置静态ip的经验 1.查看路由状态 [root@localhost ...