题目描述

Bessie likes downloading games to play on her cell phone, even though she does find the small touch 
screen rather cumbersome to use with her large hooves.She is particularly intrigued by the current g
ame she is playing. The game starts with a sequence of NN positive integers (2≤N≤248), each in the
 range 1…40. In one move, Bessie can take two adjacent numbers with equal values and replace them a
 single number of value one greater (e.g., she might replace two adjacent 7s with an 8). The goal is
 to maximize the value of the largest number present in the sequence at the end of the game. Please 
help Bessie score as highly as possible!

输入

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


题目大意

给你n个数的数列,每次可以合并两个相邻且相等的数,变成大小比原数大1的新数。问可能出现的最大的数是多少。

题解

区间dp

首先必须明确一点,如果一段区间能够按照题目要求合并成一个数,那么这个数一定是确定的。

那么可以设f[i][j]为i到j这段区间合并成的数,如果无法合并则为0。

那么就有f[i][j]=f[i][k]+1(f[i][k]==f[k+1][j]&&f[i][k]!=0)。

答案就是max{f[i][j]}。

注意需要先枚举长度再枚举起始点。

#include <cstdio>
#include <algorithm>
using namespace std;
int f[250][250];
int main()
{
int n , i , j , k , ans = 0;
scanf("%d" , &n);
for(i = 1 ; i <= n ; i ++ )
scanf("%d" , &f[i][i]);
for(i = 2 ; i <= n ; i ++ )
for(j = 1 ; j <= n - i + 1 ; j ++ )
for(k = j ; k <= i + j - 2 ; k ++ )
if(f[j][k] && f[k + 1][i + j - 1] && f[j][k] == f[k + 1][i + j - 1])
f[j][i + j - 1] = f[j][k] + 1;
for(i = 1 ; i <= n ; i ++ )
for(j = i ; j <= n ; j ++ )
ans = max(ans , f[i][j]);
printf("%d\n" , ans);
return 0;
}

【bzoj4580】[Usaco2016 Open]248 区间dp的更多相关文章

  1. bzoj4580: [Usaco2016 Open]248(区间dp)

    4580: [Usaco2016 Open]248 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 255  Solved: 204[Submit][S ...

  2. BZOJ4580: [Usaco2016 Open]248

    n<=248个数字,可以进行这样的操作:将相邻两个相同的数字合并成这个数字+1,求最大能合成多少. f(i,j)--区间i到j能合成的最大值,f(i,j)=max(f(i,k)+1),f(i,k ...

  3. P3146 [USACO16OPEN]248 (区间DP)

    题目描述  给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-40),问最大能合出多少.注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3. 这道题的思路: 状态: ...

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

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

  5. 【区间DP】【lgP3146】248

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

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

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

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

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

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

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

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

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

随机推荐

  1. (转)Ruby On Rails 推荐 Gem 列表

    作者:尘缘,QQ:130775,来源:http://www.4wei.cn/archives/1002157 PHP的包管理Composer还在刚刚兴起的阶段,Ruby社区已经有很多成熟的Gem了,R ...

  2. 海思NB-IOT模块HI2115芯片I2C通信

    1. 首先确定硬件上I2C的引脚,手册上并没有,海思技术支持说是14和15脚,我们用的是12和13脚,问题在于,如果是硬件I2C应该不能随便换个引脚吧,难道是模拟的时序? 2. 下一个奇怪的地方,这个 ...

  3. 海思NB-IOT的SDK看门狗的使用

    1. 看门狗需要喂狗,如果自己写的任务一直运行,那么空闲任务无法运行会导致看门狗复位,来看下看门狗的机制,首先系统启动的时候创建了空闲任务 在这个函数里面void vTaskStartSchedule ...

  4. Python中安装Prophet

    1. 先安装pystan依赖 按照https://pystan.readthedocs.io/en/latest/windows.html说明,请使用如下命令 conda install libpyt ...

  5. 微信小程序—day05

    小程序云服务器--环境配置 本来想要买腾讯云的云服务器,作为小程序的服务端的.无奈,腾讯云卖的太贵了,比阿里云要贵一倍,想想还是算了. 但是,没有服务端的接受,小程序的一些功能是实现不了的.找了一圈, ...

  6. 前端开发工程师 - 02.JavaScript程序设计 - 期末考试

    期末考试客观题 期末考试主观题 https://www.15yan.com/story/aY0HWAQ7oNU/     1(8分) 函数myType用于根据输入参数返回相应的类型信息. 语法如下: ...

  7. 【转】网游服务器中的GUID(唯一标识码)实现-基于snowflake算法

    本文中的算法采用twitter的snowflake算法,具体请搜索介绍,原来是用Scala写的,因我项目需要,改写成C++语言,主要用于高效的生成唯一的ID, 核心算法就是毫秒级时间(41位)+机器I ...

  8. MySQL数据库怎么截取字符串?

    函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:select left(content,200) as abstract from my ...

  9. UVa 455 - Periodic Strings - ( C++ ) - 解题报告

    1.题目大意 求一个长度不超过80的字符串的最小周期. 2.思路 非常简单,基本就是根据周期的定义做出来的,几乎不需要过脑. 3.应该注意的地方 (1) 最后输出的方式要注意,不然很容易就PE了.不过 ...

  10. 2.hbase原理(未完待续)

    hbase简介相关概念hmsterhregionserver表regionhstorememstorestorefilehfileblockcacheWALminorcompactmajorcompa ...