[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;
}

最新文章

  1. MySQL的常规操作
  2. maven依赖非maven库中jar的两种方法
  3. java中static{}语句块详解
  4. html跳转倒计时
  5. mysql的两阶段提交协议
  6. 乐在其中设计模式(C#) - 状态模式(State Pattern)
  7. 自定义radio图标
  8. 白话C#语法新特性之元组
  9. 001 java简介
  10. .Net WebApi 初探
  11. java_基础_接口和抽象类
  12. ajax阻挡设置
  13. 从ranknet到lamdarank,再到lamdamart
  14. 029_mount bind挂载
  15. Linux常用指令笔记
  16. CodeChef - CRYPCUR
  17. 前端工程师的mysql笔记
  18. 【转载】C++资源之不完全导引
  19. 不要USB数据线调试Android开发
  20. HandyJSON代码阅读

热门文章

  1. c 到 c++
  2. MySQL 8.0权限认证(下)
  3. 创建使用mysql表
  4. Jquery封装:下拉框插件
  5. 关于thinkhphp3.1中废弃 preg_replace /e 修饰符
  6. yii2中的场景使用
  7. Ubuntu18.04下MySQL8.0和Navicat15的安装与使用
  8. 学习 SQL Server (5) :视图,索引,事务和锁+T_SQL
  9. 看了Java的Class的源码,我自闭了
  10. C++ 公有继承、保护继承和私有继承的对比