[luoguP3146] [USACO16OPEN]248(区间DP)
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)的更多相关文章
- P3146 [USACO16OPEN]248 (区间DP)
题目描述 给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-40),问最大能合出多少.注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3. 这道题的思路: 状态: ...
- 【bzoj4580】[Usaco2016 Open]248 区间dp
题目描述 Bessie likes downloading games to play on her cell phone, even though she does find the small t ...
- bzoj4580: [Usaco2016 Open]248(区间dp)
4580: [Usaco2016 Open]248 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 255 Solved: 204[Submit][S ...
- 「USACO16OPEN」「LuoguP3146」248(区间dp
题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small to ...
- 「区间DP」「洛谷PP3146 」[USACO16OPEN]248 G
[USACO16OPEN]248 G 题目: 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- 又一道区间DP的题 -- P3146 [USACO16OPEN]248
https://www.luogu.org/problemnew/show/P3146 一道区间dp的题,以区间长度为阶段; 但由于要处理相邻的问题,就变得有点麻烦; 最开始想了一个我知道有漏洞的方程 ...
- [USACO16OPEN]248 G——区间dp
[USACO16OPEN]248 G 题目描述 Bessie likes downloading games to play on her cell phone, even though she do ...
- 「USACO16OPEN」「LuoguP3147」262144(区间dp
P3147 [USACO16OPEN]262144 题目描述 Bessie likes downloading games to play on her cell phone, even though ...
- 一道另类的区间dp题 -- P3147 [USACO16OPEN]262144
https://www.luogu.org/problemnew/show/P3147 此题与上一题完全一样,唯一不一样的就是数据范围; 上一题是248,而这一题是262144; 普通的区间dp表示状 ...
随机推荐
- [GDOI2014]拯救莫莉斯
题目描述 莫莉斯·乔是圣域里一个叱咤风云的人物,他凭借着自身超强的经济头脑,牢牢控制了圣域的石油市场. 圣域的地图可以看成是一个n*m的矩阵.每个整数坐标点(x , y)表示一座城市吗,两座城市间相邻 ...
- G - And Then There Was One (约瑟夫环变形)
Description Let’s play a stone removing game. Initially, n stones are arranged on a circle and numbe ...
- python调用chrome ie等浏览器
chrome: 首先,要安装下谷歌浏览器,查看谷歌浏览器的版本,对应版本下载相应的chromedriver插件,http://blog.csdn.net/huilan_same/article/det ...
- [转]TFS下的源代码控制
本文转自:http://www.cnblogs.com/ajiefj/archive/2010/04/23/1718450.html 以下主要描述了: TFS源代码控制系统的基本场景 如何把一个项目添 ...
- VMware workstation 14 CentOs 7.5.1804 虚拟机网卡设置为NAT模式并设置固定IP
一.背景知识 虚拟机网络模式 无论是vmware workstation,virtual box,virtual pc等虚拟机软件,一般来说,虚拟机有三种网络模式: 1.桥接 2.NAT 3. ...
- 微软MVC框架实战:开源的JS库Knockout
[IT168 技术] Knowckout – 当MVC遭遇MVVM Knockout (或者Knockout.js ,KnockoutJS)是一个开源的JavaScript库,网址为www.knock ...
- Greenplum开发
Greenplum(GP)采用了MPP架构,基于开源的数据库 PostgreSQL(PG). 1.首先什么是MPP架构? GreenPlum的架构采用了MPP(大规模并行处理).在 MPP 系统中,每 ...
- HDU_1006_Tick and Tick
Tick and Tick Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- R语言学习 - 线图绘制
线图是反映趋势变化的一种方式,其输入数据一般也是一个矩阵. 单线图 假设有这么一个矩阵,第一列为转录起始位点及其上下游5 kb的区域,第二列为H3K27ac修饰在这些区域的丰度,想绘制一张线图展示. ...
- CAD得到所有组名(网页版)
主要用到函数说明: _DMxDrawX::GetAllGroupName 得到所有组名. js代码实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...