[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表示状 ...
随机推荐
- MD760按键说明书
- 【1】Vim 进阶操作
一.标签 :tabnew one.c 新建标签[♥] 常用 :tabc 关闭文件 :tabp 切换前一个页面 :tabn 切换下一个页面 gt 普通模式下操作 常用 二.窗口 :sp 水平 ...
- 网络KPI异常检测之时序分解算法
时间序列数据伴随着我们的生活和工作.从牙牙学语时的“1, 2, 3, 4, 5, ……”到房价的走势变化,从金融领域的刷卡记录到运维领域的核心网性能指标.时间序列中的规律能加深我们对事物和场景的认识, ...
- ecshop头部添加所在城市
首先,在/includes/lib_main.php中,找到代码:function assign_template($ctype = '', $catlist = array()) ,在方法中添加 ...
- STM32的ADC采样时间
STM32的ADC采样时间与其ADC的时钟频率密不可分. 例:STM32F103系列的ADC的时钟是在APB2(最大72MHZ)上.我们可以对其分频: RCC_PCLK2_Div2: ADC cloc ...
- jenkins环境安装(windows)
一.简介 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. 二. Jenkins功能 1. ...
- 2019-02-04 Linux的一些常用命令学习
今天在电脑里用装了个ubuntu的虚拟机,学习了一下基本操作 ls ls用于查看文件夹的文件 mkdir mkdir可以创建一个文件夹(directory) cd 目录切换 pwd print wor ...
- WebDriverWait
显示等待 WebDriverWait( driver, timeout, poll_frequency, ignored_exceptions) driver: 传入WebDriver实例,即webd ...
- Web api配置填坑攻略
最近开始使用web api,开发调试过程还算顺利,现在项目已经发布,网站已经部署,结果浏览过程出现问题(不出问题好像不正常吧……),做个note开始填坑. 1.1号坑 咋一开始就爆出另一个程序正在使用 ...
- cb52a_c++_STL_堆排序算法make_push_pop_sort_heap
cb52a_c++_STL_堆排序算法make_push_pop_sort_heapheapsort堆排序算法make_heap()-特殊的二叉树,每一个节点都比根小,根就是最大的数.大根堆,也可以做 ...