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. js实现右击

    <!DOCTYPE html> <html>     <head>  <meta charset="UTF-8">  <tit ...

  2. nagios维护之添加监控

    查看修改的配置是否有误: /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 一.nagios监控交换机 编辑 /usr/ ...

  3. 解决镜像无法删除的问题multiple repositories

    Error response from daemon: conflict: unable to delete ea5f89e79b1e (must be forced) - image is refe ...

  4. Ant 之 Task

    Ant提供了大量的核心task和可选task,除此之外,Ant还允许用户定义自己的task,这大大扩展了Ant的功能.本书由于篇幅关系,所以不可能详细介绍Ant所有的核心task和可选task,本书将 ...

  5. TOJ 3589 likaer的最长点距

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3589 时间限制(普通/Jav a) ...

  6. 微信小程序开发小技巧——单击事件传参、动态修改样式、轮播样式修改等

    一. 脚本部分: 1. 表达式无效的处理: 如果你发现自己编写的表达式无效或者数据不展示,那么请先检查你的表达式是否有添加{{}},小程序中全部都要添加的,只要是在模板中调用js中的数据 2. 获取元 ...

  7. ubuntu下nginx的常用操作

    1.安装不多说了,我是使用apt-get进行安装的,直接键入 apt-get install nginx 2.启动nginx. 首先找到nginx的主运行程序(相当于windows下的nginx.ex ...

  8. SQL Server 触发器 表的特定字段(一个字段)更新时,触发Update触发器

    CREATE TRIGGER [dbo].[Trg_Update_table1_column1]   on table1  after update  as  if update (column1)  ...

  9. [leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  10. 自定义进度条渐变色View

    package com.jianke.stepCounter.Activity; import android.annotation.SuppressLint; import android.cont ...