洛谷传送门
BZOJ传送门
题目描述
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 NN positive integers ( 2 ≤ N ≤ 262 , 144 2 \leq N\leq 262,144 2≤N≤262,144), each in the range 1 … 40 1 \ldots 40 1…40. In one move, Bessiecan take two adjacent numbers with equal values and replace them asingle number of value one greater (e.g., she might replace two adjacent 7 7 7 with an 8 8 8). The goal is to maximize the value of thelargest number present in the sequence at the end of the game. Pleasehelp Bessie score as highly as possible!
Bessie喜欢在手机上下游戏玩(……),然而她蹄子太大,很难在小小的手机屏幕上面操作。
她被她最近玩的一款游戏迷住了,游戏一开始有 n n n个正整数,( 2 ≤ n ≤ 262144 2\le n\le 262144 2≤n≤262144),范围在 1 − 40 1-40 1−40。在一步中,贝西可以选相邻的两个相同的数,然后合并成一个比原来的大一的数(例如两个 7 7 7合并成一个 8 8 8),目标是使得最大的数最大,请帮助Bessie来求最大值。
输入输出格式
输入格式:
The first line of input contains N N N, and the next N N N lines give the sequence of N N N numbers at the start of the game.
输出格式:
Please output the largest integer Bessie can generate.
输入输出样例
输入样例#1:
4
1
1
1
2
输出样例#1:
3
说明
In this example shown here, Bessie first merges the second and third
1
1
1 to obtain the sequence 1 2 2
, and then she merges the
2
2
2 into a
3
3
3. Note that it is not optimal to join the first two
1
1
1.
解题分析
很妙的一道题, 设 d p [ i ] [ j ] dp[i][j] dp[i][j]表示以 j j j位置开始合并为 i i i的结尾位置, 则 d p [ i ] [ j ] = d p [ i − 1 ] [ d p [ i − 1 ] [ j − 1 ] ] dp[i][j]=dp[i-1][dp[i-1][j-1]] dp[i][j]=dp[i−1][dp[i−1][j−1]], 初始化 d p [ v a l ] [ i ] = i + 1 dp[val][i]=i+1 dp[val][i]=i+1。
看起来有点像倍增的感觉…
代码如下:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <cmath>
#define R register
#define IN inline
#define W while
#define gc getchar()
#define MX 300500
template <class T>
IN void in(T &x)
{
x = 0; R char c = gc;
for (; !isdigit(c); c = gc);
for (; isdigit(c); c = gc)
x = (x << 1) + (x << 3) + c - 48;
}
int dp[59][MX], n, ans;
int main(void)
{
in(n); int buf;
for (R int i = 1; i <= n; ++i) in(buf), dp[buf][i] = i + 1;
for (R int i = 1; i <= 58; ++i)
for (R int j = 1; j <= n; ++j)
{
if(!dp[i][j]) dp[i][j] = dp[i - 1][dp[i - 1][j]];
if(dp[i][j]) ans = i;
}
printf("%d", ans);
}