[USACO16OPEN]262144
Description
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), each in the range 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 twoadjacent 7s with an 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个正整数,(2<=n<=262144),范围在1-40。在一步中,贝西可以选相邻的两个相同的数,然后合并成一个比原来的大一的数(例如两个7合并成一个8),目标是使得最大的数最大,请帮助Bessie来求最大值。
Input
The first line of input contains N, and the next N lines give the sequence
of N numbers at the start of the game.
Output
Please output the largest integer Bessie can generate.
Sample Input
4 1 1 1 2
Sample Output
3
Hint
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 2s into a 3. Note that it is
not optimal to join the first two 1s.
思路
首先这一题有个弱化版 ;
那么,这题是一道区间 $dp$ 的题;
但实际上数据范围过大,不能使用一般的表示左右断点 ;
所以,我们可以设 $dp[i][j]$ 表示合成 $i$ 这个数 ,左端点是 $j$,能合成的最远的右端点是 $dp[i][j]$ ;
那么转移方程就是
$dp[i][j]=dp[i-1][dp[i-1][j]+1] $ ;
如样例
$1$ $1$ $1$ $2$ ;
$dp[3][2]=dp[2][dp[2][2]+1]$ ;
然后统计最大的 $i$ ;
就可以了;
代码
#include<bits/stdc++.h> #define re register typedef long long ll; using namespace std; inline ll read() { ll a=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();} return a*f; } ll n,ans; ll a[300010]; ll dp[101][300010]; int main() { ll mx=0; n=read(); for(re ll i=1;i<=n;i++) { a[i]=read(); mx=max(mx,a[i]);//寻找最大值为了赋初值时少几层循环 } for(re ll i=1;i<=mx;i++) for(re ll j=1;j<=n;j++) { if(a[j]==i) dp[i][j]=j;//赋初值,每个数都能合成自己本身,左右端点就是当前位置 } for(re ll i=2;i<=100;i++) { for(re ll j=1;j<=n;j++) { if(!dp[i-1][j])// i-1 这个数不可合成 continue; dp[i][j]=max(dp[i-1][dp[i-1][j]+1],dp[i][j]);//转移方程 if(dp[i][j]) ans=max(i,ans); //统计能合成的最大数 } } printf("%lld\n",ans); //return 0; }