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

  1. 状态压缩---区间dp第一题

    标签: ACM 题目 Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is ...

  2. poj 2955 Brackets (区间dp基础题)

    We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...

  3. 洛谷P3146 [USACO16OPEN]248

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

  4. 洛谷 P3146 [USACO16OPEN]248

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

  5. P3146 [USACO16OPEN]248

    P3146 [USACO16OPEN]248 题解 第一道自己码出的区间DP快庆祝一哈 2048 每次可以合并任意相邻的两个数字,得到的不是翻倍而是+1 dp[L][R] 区间 L~R 合并结果 然后 ...

  6. 再一道区间DP -- P4170 [CQOI2007]涂色

    https://www.luogu.org/problemnew/show/P4170 一道简单的区间DP,注意读入 #include <bits/stdc++.h> #define up ...

  7. poj 2955 区间dp入门题

    第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i ...

  8. 二叉树问题(区间DP好题)

    二叉树问题 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Petya Bulochkin很幸运:他得到了一份在"Macrohard"公司的工作.他想要展现他的才华, ...

  9. codeforces 1140D(区间dp/思维题)

    D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

随机推荐

  1. opencv: Rotate image by 90, 180 or 270 degrees

    opencv2: void rotate_cw(const cv::Mat& image, cv::Mat& dest, int degrees) { ) { : dest = ima ...

  2. 连接SQL常见问题

    HTTP Status 500 – Internal Server Error Type Exception Report Message Request processing failed; nes ...

  3. CentOS查看进程、杀死进程、启动进程等常用命令

    关键字: linux 查进程.杀进程.起进程 1.查进程     ps命令查找与进程相关的PID号:     ps a 显示现行终端机下的所有程序,包括其他用户的程序.     ps -A 显示所有程 ...

  4. JSP跳转到指定位置

    通常情况下用如下写法即可 onclick='window.location.hash = "某元素的id";' 但我的元素是动态生成的,直接生成代码附带多个单引号和双引号,js语言 ...

  5. RSA加密原理使用方式签名验证

      RSA加密原理使用方式签名验证 加密是网络传输中非常重要的一环,它保证了信息的安全性,让他人无法通过抓包来获取通讯的信息也无法通过伪造信息而实现对系统的入侵.其中最为常用的信息传递加密方式就是RS ...

  6. swift 8.0之后打开 手机设置

    if #available(iOS 8.0, *){ if let url = URL(string: UIApplication.openSettingsURLString), UIApplicat ...

  7. xampp配置多个监听端口和不同的网站目录

    1.配置Apache文件httpd.conf 打开xampp安装目录下的Apache->conf文件夹下的httpd.conf,用记事本打开 首先在Listen 80端口下添加其他监听端口: L ...

  8. 建立SSH的信任关系

    1.在Client上root用户执行ssh-keygen命令,生成建立安全信任关系的证书.  Client端 # ssh-keygen -t rsa Generating public/private ...

  9. 在 JavaScript 中 ["1","2","3"].map(parseInt) 为何返回不是 [1,2,3] 却是 [1,NaN,NaN]?

    这个问题我是希望有很多人可以一起交流的: 我在 http://blog.csdn.net/justjavac/article/details/19473199#t0 上看到了比较详细的解释, 但是具体 ...

  10. MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring

    Mybatis在与Spring集成的时候可以配置 MapperFactoryBean来生成Mapper接口的代理. 例如 <bean id="userMapper" clas ...