[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表示状 ...
随机推荐
- java关键字tranisent用法详解
作为java中的一个关键字,tranisent用的并不是很多,但是在某些关键场合,却又起着极为重要的作用,因此有必要对它进行一些必要的了解. 一.定义:声明不用序列化的成员域.(源自百度百科) 二.作 ...
- TB6612FNG电机驱动模块
TB6612FNG电机驱动模块 模块原理图 模块的使用 TB6612是双驱动,也就是可以驱动两个电机 下面分别是控制两个电机的IO口 STBY口接单片机的IO口清零电机全部停止, 置1通过AIN1 A ...
- 「雅礼集训 2017 Day4」洗衣服
题目 点这里看题目. 分析 首先考虑只有洗衣机的情况.我们可以想到,当前洗衣任务结束越早的洗衣机应该被先用,因此可以用堆来动态维护. 再考虑有烘干机的情况.很显然,越晚洗完的衣服应该越早烘 ...
- 第m大的身份证号码(局部排序代全局、结构体排序)
第m大的身份证号码(点击) 时间限制: 1 Sec 内存限制: 128 MB ...
- DML_Modifying Data Through Table Expressions_UPDATE
DML_Modifying Data Through Table Expressions_UPDATE之前也学习过使用CTE,再来泛泛的学习下,最后将会将一些书籍上或学习到的CTE来个小结 /* Mi ...
- c常用函数-strchr和strrchr
strchr和strrchr strrchr函数用于查找指定字符在一个字符串中最后一次出现的位置,然后返回指向该位置的指针 strchr函数用于查找指定字符在一个字符串中第一次出现的位置,然后返回指向 ...
- 百度文本编辑器的toolbars属性值描述
toolbars: [ [ 'anchor', //锚点 'undo', //撤销 'redo', //重做 'bold', //加粗 ...
- CODING DevOps 系列第三课:云计算、云原生模式下 DevOps 的建设
本文首先会和大家分享当前整个应用生命周期的演变历程,然后讲解云计算模式下 DevOps 建设包含的过程.流程规范和标准,最后讲解云原生时代到来会带来哪些改变,以及标准化的建设会有哪些改变和突破. 应用 ...
- 4.kubernetes的服务发现插件-CoreDNS
1.1.部署K8S内网资源清单http服务 1.2.部署coredns 部署K8S内网资源清单http服务 在运维主机HDSS7-200.host.com上,配置一个nginx虚拟主机,用以提高k8s ...
- 基于NACOS和JAVA反射机制动态更新JAVA静态常量非@Value注解
1.前言 项目中都会使用常量类文件, 这些值如果需要变动需要重新提交代码,或者基于@Value注解实现动态刷新, 如果常量太多也是很麻烦; 那么 能不能有更加简便的实现方式呢? 本文讲述的方式是, 一 ...