USACO 262144
作者:互联网
USACO 262144
Description
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 game she is playing. The game starts with a sequence of N positive integers (2≤N≤262,144), 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!
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的一个变形。
首先262144这个数肯定不同凡响,教给我们一个新常数。
它是2的18次幂呀。
所以,它的最优秀的合并,答案也绝对不能超过40+18=58.
最优秀的合并就是每两个数合并,然后合并出的两个数又能再合并,像二叉树一样地一层层往上合并。
那么区间DP的复杂度是承载不了这个数据范围的,我们考虑优化。
我们不应该把区间DP想象的那么狭义。
定义\(f[i][j]\)表示左端点为j,能拼出i这个数字的右端点位置。
这样的话,转移方程就是:
\[f[i][j]=f[i-1][f[i-1][j]] \]比较好理解吧。
然后是代码:
#include<bits/stdc++.h>
using namespace std;
const int N=262145,M=60;
int a[N],f[M][N],ans;
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
f[a[i]][i]=i+1;
}
for(int i=1;i<=58;i++)
for(int j=1;j<=n;j++)
{
f[i][j]=(!f[i][j]?f[i-1][f[i-1][j]]:f[i][j]);
ans=(f[i][j]?i:ans);
}
printf("%d",ans);
return 0;
}
标签:sequence,int,合并,USACO,262144,game,she,Bessie 来源: https://www.cnblogs.com/fusiwei/p/13813793.html