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. 关于RAID的概述

    Raid 0:一块硬盘或者以上就可做raid0优势:数据读取写入最快,最大优势提高硬盘容量,比如3快80G的硬盘做raid0 可用总容量为240G.速度是一样.缺点:无冗余能力,一块硬盘损坏,数据全无 ...

  2. vue 添加子路由,并对路由重定向

    // 用户信息首页 { path: '/user/index', name: 'userIndex', component: userIndex, redirect: '/user/index/sho ...

  3. HDU 6315 Naive Operations(线段树区间整除区间)

    Problem DescriptionIn a galaxy far, far away, there are two integer sequence a and b of length n.b i ...

  4. Linux系统下重启Tomcat

    在Linux系统下,重启Tomcat使用命令操作的! 首先,进入Tomcat下的bin目录 cd /usr/local/tomcat/bin 使用Tomcat关闭命令 ./shutdown.sh 查看 ...

  5. Bootstrap(10) 进度条媒体对象和 Well 组件

    一.Well 组件这个组件可以实现简单的嵌入效果. <!-- //嵌入效果 --> <div class="well">Bootstrap</div& ...

  6. 史上最全的MSSQL复习笔记

    1.什么是SQL语句 SQL语言,结构化的查询语言(Structured Query Language),是关系数据库管理系统的标准语言.它是一种解释语言,写一句执行一句,不需要整体编译执行. 语法特 ...

  7. Windows删除服务方法

  8. 初次搭建spring-boot 整合ssm(有许多小坑)

    首先,我是采用官网下载,版本最好选择1.5.16的(这是重点) 下载完毕后,用idea打开解压后的项目. 1.整合spring-mvc 在pom.xml中加入web依赖 <dependency& ...

  9. linux下的压缩命令

    linux zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip - ...

  10. 5I - 汉诺塔IV

    还记得汉诺塔III吗?他的规则是这样的:不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允许大盘放到小盘的上面.xhd在想如果我们允许最大的盘子放到最上面会怎么样 ...