题目描述

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. 【BZOJ1176】[BOI2007]Mokia 摩基亚

    [BZOJ1176][BOI2007]Mokia 摩基亚 题面 bzoj 洛谷 题解 显然的\(CDQ\)\(/\)树套树题 然而根本不想写树套树,那就用\(CDQ\)吧... 考虑到点\((x1,y ...

  2. spring boot 数据库连接

    server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/jdjk?serverTimezone=Asia/Sha ...

  3. 创龙DSP6748学习之RS485收发

    1. 先看下原理图,第一个问题,RS485其实就是使用的串口USART1,同时485的输出脚之间接120欧姆的电阻. 遇到个问题,为什么有两个使能引脚?还有RS485_A和RS485_B为什么分别接上 ...

  4. pg mysql 比较

    PostgreSQL 与 MySQL比较: 一.pg优势: MySQL 不支持 OVER 子句, 而 Pg 支持 它可以存储 array 和 json, 可以在 array 和 json 上建索引, ...

  5. lunix安装

    https://www.cnblogs.com/wcwen1990/p/7630545.html

  6. 为什么测试人员必须掌握Linux?

    相信点进来的小伙伴不是对Linux感兴趣就是对测试感兴趣了,也希望本文可以帮助之前接触过Linux的小伙伴找到继续坚持学习下去的动力,之前没接触过Linux的小伙伴也能找到开始学习Linux的兴趣. ...

  7. jenkins--Jenkins+Git+coding+maven 实现自动化测试持续集成

    1.打开Jenkins官网,下载jenkins.war https://jenkins.io/download/ 2.将该war包直接放置到Tomcat的webapp下. 3.查看自己Tomcat的端 ...

  8. Unity编辑器 - TreeView控件笔记

    用起来有一些规则,写个简单的案例以备查阅: using System.Collections.Generic; using UnityEditor.IMGUI.Controls; using Unit ...

  9. Anyproxy抓包工具

    1.安装Nodejs和AnyProxy以及安装模拟器(移动端抓包工具) 第一步:nodejs下载地址:http://nodejs.cn/download/ 下载Windows版本,直接运行安装即可, ...

  10. Vue动画效果

    1.哪些元素/那些组件适合在那些条件下实现动画效果 条件渲染 (使用 v-if) 条件展示 (使用 v-show) 动态组件 组件根节点 简单经典例子:(文字隐藏到显示效果) <div> ...