[USACO16OPEN]248 G

题目描述

Bessie likes downloading games to play on her cell phone, even though she doesfind the small touch screen rather cumbersome to use with her large hooves.

She is particularly intrigued by the current game she is playing.The game starts with a sequence of \(N\) positive integers \((2\leq N\leq 248)\), each in the range \(1...40\). In one move, Bessie cantake two adjacent numbers with equal values and replace them a singlenumber of value one greater (e.g., she might replace two adjacent \(7\)s with an \(8\) ). The goal is to maximize the value of the largest numberpresent in the sequence at the end of the game. Please help Bessiescore as highly as possible!

给定一个 \(1\times n\) 的地图,在里面玩 \(2048\),每次可以合并相邻两个(数值范围 \(1-40\)),问最大能合出多少。注意合并后的数值并非加倍而是 \(+1\),例如 \(2\) 与 \(2\) 合并后的数值为 \(3\) 。

输入格式

The first line of input contains \(N\), and the next \(N\) lines give the sequence

of \(N\) numbers at the start of the game.

输出格式

Please output the largest integer Bessie can generate.

输入输出样例

输入

4
1
1
1
2

输出

3

说明/提示

In this example shown here, Bessie first merges the second and third 1s to obtain the sequence \(1 2 2\), and then she merges the \(2\)s into a

\(3\). Note that it is not optimal to join the first two \(1\)s.

题目简介

博主本人看英语看得也难受,在这里简单解释一下吧。

就是给一个长度为 \(n\) 的序列,相邻并相同数字之间可以合并,合并后的值为原来值 \(+1\),不相同的值不允许合并,跟之前的石子合并不同,求怎么合并能够使合并后的序列中最大值最大。

数组含义

\(a[i]\): 原序列。

\(f[i][j]\): 从 \(i\) 到 \(j\) 的序列合并后的值。例如样例中 \(f[2][3]=1+1=2\) 。

基本思路

阶段:枚举宽度为 \(d\) 的序列。

状态:枚举 \(i\) 和 \(j\),从 \(i\) 到 \(j\) 的序列。枚举 \(k\),将从 \(i\) 到 \(j\) 的序列分为两段。

决策:若 \(f[i][k]==f[k+1][j]\),则可以合并,与 \(f[i][j]\) 取最大值。

动态转移方程:

if(f[i][k]==f[k+1][j]&&f[i][k]!=0&&f[k+1][j]!=0){
f[i][j]=max(f[i][j],f[i][k]+1);
ans=max(ans,f[i][j]);
}

注意

初始化 \(f[i][i]=a[i]\)。

若 \(f[i][k]==f[k+1][j]\),但是两个都为 \(0\),不可以合并。

代码

#include <bits/stdc++.h>
using namespace std; const int maxn=250+50;
int n;
int a[maxn];
int f[maxn][maxn];
int ans; int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
f[i][i]=a[i];
}
for(int d=2;d<=n;d++){
for(int i=1,j;(j=i+d-1)<=n;i++){
for(int k=i;k<j;k++){
if(f[i][k]==f[k+1][j]&&f[i][k]!=0&&f[k+1][j]!=0){
f[i][j]=max(f[i][j],f[i][k]+1);
ans=max(ans,f[i][j]);
}
}
}
}
printf("%d\n",ans);
return 0;
}

[USACO16OPEN]248 G——区间dp的更多相关文章

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

    传送门 f[i][j]表示区间 i-j 合并的最大值 转移: 若f[i][k] && f[k+1][j] && f[i][k] == f[k+1][j] --> ...

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

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

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

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

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

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

  5. 【笔记】区间DP

    记录一些基础的区间 \(\text{DP}\) 题. 0x00 AT_dp_n N - Slimes 最板的区间 \(\text{DP}\) . 记 \(f[i][j]\) 表示合并 \(i\sim ...

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

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

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

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

  8. 【动态规划DP】[USACO16OPEN]248

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

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

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

随机推荐

  1. java实现第六届蓝桥杯生命之树

    生命之树 生命之树 在X森林里,上帝创建了生命之树. 他给每棵树的每个节点(叶子也称为一个节点)上,都标了一个整数,代表这个点的和谐值. 上帝要在这棵树内选出一个非空节点集S,使得对于S中的任意两个点 ...

  2. Linux目录处理命令mkdir详解

    mkdir(英文原意:make directories),基本作用是创建新的目录,命令的路径及权限: 可以看到,这个命令的路径是/usr/bin/mkdir,所以它的执行权限是所有用户 mkdir 创 ...

  3. CDN HTTPS安全加速基本概念、解决方案及优化实践

    大家都知道,HTTP 本身是明文传输的,没有经过任何安全处理,网站HTTPS解决方案通过在HTTP协议之上引入证书服务,完美解决网站的安全问题.本文将为大家介绍阿里云CDN HTTPS安全加速传输的基 ...

  4. OC语言-NSMutableArray为什么要用strong来修饰

    Talk is cheap show you my code!  NSMutableArray属性为什么要用strong来修饰,其实就是一个深复制和浅复制的问题. <pre name=" ...

  5. PMBOK 基础知识(1)

    启动.结束过程 项目管理计划 第一章  引论 第2章项目运行环境 第3章 项目经理的角色 第4章 项目整合管理 第5章 项目范围管理 第6章 项目进度管理 第7章 项目成本管理 第8章 项目质量管理  ...

  6. Postgresql DB安装和使用问题记录

    2.选择语言后提示: Error: There has been an error. Please put SELinux in permissive mode and then run instal ...

  7. @topcoder - SRM614D1L3@ TorusSailing

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 N*M 的方格图,某人从 (0, 0) 出发想要走到 ...

  8. Tensorflow从0到1(2)之基础知识

    张量 张量是tensorflow中的基本数据结构 # 全零张量 zero_tsr = tf.zeros([row_dim, col_dim]) # 全1张量 ones_tsr = tf.ones([r ...

  9. Java中的map集合顺序如何与添加顺序一样

    一般使用map用的最多的就是hashmap,但是hashmap里面的元素是不按添加顺序的,那么除了使用hashmap外,还有什么map接口的实现类可以用呢? 这里有2个,treeMap和linkedH ...

  10. 开启PG的归档模式

    目录 开启PG的归档模式 1.查看pg的数据目录 2.查看pg的归档情况 3.查看归档的模式和位置 4.建立归档目录 5.配置归档参数 6.重启pg 7.查看&&切换归档日志 8.查看 ...