题目描述

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. javascript sorting/ v8 sorting

    https://github.com/mgechev/v8-sorting-test/tree/master/src/algorithms

  2. 成都Uber优步司机奖励政策(3月8日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. MySQL高级-showProfile

    一.Show Profile是什么? 是MySql提供可以用来分析当前会话中语句执行的资源消耗情况,可以用于SQL的调优的测量. 默认情况下,参数处于关闭状态,并保存最近15次的运行结果. 二.分析步 ...

  4. FCL中你不得不知的几种委托

    FCL中丰富的类库信息极大的方便了我们的编码,很多我们日常经常用到的类型,FCL中已经帮我们定义好,下面要介绍的就是FCL中定义好的几种委托类型,直接使用它们不仅能提高我们的编码效率,而且还能让我们的 ...

  5. Java开发工程师(Web方向) - 04.Spring框架 - 第5章.Web框架

    第5章--Web框架 Web框架概述 Web框架单元测验 本次得分为:13.50/15.00, 本次测试的提交时间为:2017-09-25 1单选(2分) 关于Spring MVC中Dispatche ...

  6. 【带 josn参数的测法】

    遇到json 参数的情况这样写  ,否则就会报错 cod 415   nocookie post请求 ,","email":"beihe@163.com&quo ...

  7. 复合词 (Compund Word,UVa 10391)

    题目描述: 题目思路: 用map保存所有单词赋键值1,拆分单词,用map检查是否都为1,即为复合词 #include <iostream> #include <string> ...

  8. Python-3.6 安装pycrypto 2.6

    最近接触公司后台管理系统的开发,其中涉及到加密模块pycrypto. 重点来了!!!!敲黑板!!!! pycrypto在PyCharm中跟其他的模块不一样,pip install pycrypto安装 ...

  9. Android开发-API指南-<uses-permission>

    <uses-permission> 英文原文:http://developer.android.com/guide/topics/manifest/uses-permission-elem ...

  10. Tunnel上传遇到字符[NUL]问题

    模拟生产环境下数据格式,再现异常情景:   Notepad++怎样输入字符[NUL]? 安装 Hex-Editor 插件: HexEditor插件用于在notepad++中查看16进制文件,只需要将此 ...