[USACO16OPEN]248 G——区间dp
[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的更多相关文章
- [luoguP3146] [USACO16OPEN]248(区间DP)
传送门 f[i][j]表示区间 i-j 合并的最大值 转移: 若f[i][k] && f[k+1][j] && f[i][k] == f[k+1][j] --> ...
- 「区间DP」「洛谷PP3146 」[USACO16OPEN]248 G
[USACO16OPEN]248 G 题目: 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- 「USACO16OPEN」「LuoguP3146」248(区间dp
题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small to ...
- 「USACO16OPEN」「LuoguP3147」262144(区间dp
P3147 [USACO16OPEN]262144 题目描述 Bessie likes downloading games to play on her cell phone, even though ...
- 【笔记】区间DP
记录一些基础的区间 \(\text{DP}\) 题. 0x00 AT_dp_n N - Slimes 最板的区间 \(\text{DP}\) . 记 \(f[i][j]\) 表示合并 \(i\sim ...
- 又一道区间DP的题 -- P3146 [USACO16OPEN]248
https://www.luogu.org/problemnew/show/P3146 一道区间dp的题,以区间长度为阶段; 但由于要处理相邻的问题,就变得有点麻烦; 最开始想了一个我知道有漏洞的方程 ...
- 【bzoj4580】[Usaco2016 Open]248 区间dp
题目描述 Bessie likes downloading games to play on her cell phone, even though she does find the small t ...
- 【动态规划DP】[USACO16OPEN]248
题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small to ...
- 一道另类的区间dp题 -- P3147 [USACO16OPEN]262144
https://www.luogu.org/problemnew/show/P3147 此题与上一题完全一样,唯一不一样的就是数据范围; 上一题是248,而这一题是262144; 普通的区间dp表示状 ...
随机推荐
- Mysql添加索引及索引的优缺点
一.什么是索引? 索引是对数据库表中的一列或多列值进行排序的一种结构,使用索引可以快速访问数据库表中的特定信息. 二.索引的作用? 索引相当于图书上的目录,可以根据目录上的页码快速找到所需的内容,提高 ...
- IDEA自定义模板
RT,虽然看起来简单,每当配置新的IDEA 时,又不免度娘,所以整理下 1.类的模板 路径: settings-Editor-File and Code Templates 右侧找到 class 添加 ...
- Tomcat/ WebSphere/WebLogic的作用和特点
作用: Tomcat:目前应用非常广泛的免费web服务器,支持部分j2ee. WebSphere:是IBM集成软件平台.可做web服务器,WebSphere提供了可靠.灵活和健壮的集成软件. Webl ...
- CISCN 2019-ikun
0x01 进去网址,页面如下: 刚开始有个登陆和注册的按钮,上图是我已经注册后登陆成功后的页面,我们发现在图的左下角给了一个关键的提示,购买LV6,通过寻找我们发现页面数很多,大概500页,一个一个找 ...
- 基于Web的监控系统的开发进行分布式和现代生产(外文翻译)
摘要 近年来,Web技术发展迅速.尤其是网络浏览器增强了其功能因为JavaScript,CSS3和HTML5的改进.因此,功能越来越丰富的基于Web的软件解决方案功能范围可用.通过使用响应式网页设计( ...
- numpy中np.array()与np.asarray的区别以及.tolist
array 和 asarray 都可以将 结构数据 转化为 ndarray,但是主要区别就是当数据源是ndarray时,array仍然会copy出一个副本,占用新的内存,但asarray不会. 1.输 ...
- jmeter组件中 测试计划,线程组,sampler等等
[测试计划] 这边用户定义的变量,定义整个测试中使用的重复值(全局变量),一般定义服务器的ip,端口号 [线程组] 关于,线程组,我简单聊聊,有不对的地方欢迎大家拨乱反正 线程数:你需要运行的线程 比 ...
- #linux vscode 保存总提示“Retry as sudo”
linux中,对不同路径下的文件,系统默认指定了不同的操作权限(读/写/执行),出现这个问题是由于文件的权限不足造成的.(路径为/opt/lampp/htdocs/LearnPHP_jayce/hel ...
- Centos 7使用systemctl补全服务名称
使用jsw将程序打包成服务后,发现不能使用service + 服务名前几个字母 + tab 快捷键补全服务名,但是tab键可以补全文件夹名,翻阅了各个文档后,最终还是找到了问题所在. 本人安装的是Ce ...
- JavaWeb网上图书商城完整项目--day02-2.regist页面输入框得到焦点隐藏label
实现当光标输入在输入输入框的时候,将后面的内容隐藏,例如在用户名称输入信息的时候,后面的用户名不能为空隐藏 我们来看看regist.js的代码: //该函数在html文档加载完成之后会调用 $(fun ...