传送门

f[i][j]表示区间 i-j 合并的最大值

转移:

若f[i][k] && f[k+1][j] && f[i][k] == f[k+1][j] --> f[i][j] = max(f[i][k]+1,f[i][j])

但要注意, 若f[i][k]!=f[k+1][j],那么无法进行转移

代码

#include <cstdio>
#include <iostream>
#define max(x, y) ((x) > (y) ? (x) : (y)) int n, ans;
int f[5001][5001]; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} int main()
{
int i, j, k;
n = read();
for(i = 1; i <= n; i++) f[i][i] = read(), ans = max(ans, f[i][i]);
for(i = n - 1; i >= 1; i--)
for(j = i + 1; j <= n; j++)
{
for(k = i; k < j; k++)
if(f[i][k] && f[k + 1][j] && f[i][k] == f[k + 1][j])
f[i][j] = max(f[i][j], f[i][k] + 1);
ans = max(ans, f[i][j]);
}
printf("%d\n", ans);
return 0;
}

  

[luoguP3146] [USACO16OPEN]248(区间DP)的更多相关文章

  1. P3146 [USACO16OPEN]248 (区间DP)

    题目描述  给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-40),问最大能合出多少.注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3. 这道题的思路: 状态: ...

  2. 【bzoj4580】[Usaco2016 Open]248 区间dp

    题目描述 Bessie likes downloading games to play on her cell phone, even though she does find the small t ...

  3. bzoj4580: [Usaco2016 Open]248(区间dp)

    4580: [Usaco2016 Open]248 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 255  Solved: 204[Submit][S ...

  4. 「USACO16OPEN」「LuoguP3146」248(区间dp

    题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small to ...

  5. 「区间DP」「洛谷PP3146 」[USACO16OPEN]248 G

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

  6. 又一道区间DP的题 -- P3146 [USACO16OPEN]248

    https://www.luogu.org/problemnew/show/P3146 一道区间dp的题,以区间长度为阶段; 但由于要处理相邻的问题,就变得有点麻烦; 最开始想了一个我知道有漏洞的方程 ...

  7. [USACO16OPEN]248 G——区间dp

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

  8. 「USACO16OPEN」「LuoguP3147」262144(区间dp

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

  9. 一道另类的区间dp题 -- P3147 [USACO16OPEN]262144

    https://www.luogu.org/problemnew/show/P3147 此题与上一题完全一样,唯一不一样的就是数据范围; 上一题是248,而这一题是262144; 普通的区间dp表示状 ...

随机推荐

  1. bzoj 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛【树形dp】

    设f[u][0/1]为u这个点不选/选,转移的时候从儿子转移,f[u][1]=sum(f[son][0])+1,f[u][0]=sum(max(f[son][0],f[e[i].to][1])) #i ...

  2. mycat登录报错Host 'XXX' is blocked because of many connection errors的另一种解决思路

    报错时机 使用了mycat,而不是单纯使用了mysql. 报错信息 ERROR 1129 (HY000): Host '1.23.22.18' is blocked because of many c ...

  3. 洛谷 P2142 高精度减法(模板)

    题目描述 高精度减法 输入输出格式 输入格式: 两个整数a,b(第二个可能比第一个大) 输出格式: 结果(是负数要输出负号) 输入输出样例 输入样例#1: 2 1 输出样例#1: 1 说明 20%数据 ...

  4. HDU 5514 欧拉函数应用

    前置技能: <=i且与i互质的数的和是phi(i)*i/2 思路: 显然每个人的步数是gcd(a[i],m) 把m的所有因数预处理出来 1~m-1中的每个数 只会被gcd(m,i)筛掉一遍 // ...

  5. Android项目模块化遇到的问题

    1.问题背景 gradle 4 MacOs 10.14.3 Android Studio 3 在android模块化的时候,例如,有两个模块,一个是usercenter,另一个是common. 其中u ...

  6. 题解报告:hdu 1010 Tempter of the Bone

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Problem Description The doggie found a bone in a ...

  7. excel 录入oracle 方法

    http://blog.itpub.net/28602568/viewspace-1797410/ 1.方法1:外部表 1.将excel另存为.txt格式(2种txt 格式都可以),再传到数据库dir ...

  8. Spark学习笔记1:Application,Driver,Job,Task,Stage理解

    看了spark的原始论文和相关资料,对spark中的一些经常用到的术语做了一些梳理,记录下. 1,Application application(应用)其实就是用spark-submit提交的程序.比 ...

  9. LN : leetcode 231 Power of Two

    lc 231 Power of Two 231 Power of Two Given an integer, write a function to determine if it is a powe ...

  10. overflow实现隐藏滚动条同时又可以滚动

    .scroll-list ul{ white-space: nowrap; -webkit-overflow-scrolling: touch; overflow-x: auto; overflow- ...