4580: [Usaco2016 Open]248

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 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!

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
//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.
这道题由于我英语不好的原因让我卡在题意上了。。。。。
题意:给一串序列,如果相邻两个数相同就可以把他们变为一个比它们大一的数,如两个7可变为一个8;问题是我们要使最后的序列中的最大值尽可能的大,请输出这个最大的最大值。
题解:这个还是蛮好做的,用f[i][j]表示这段区间可转变成的最大的数字,同时用一个变量k把区间分为两段,若f[i][k]=f[k+1][j]就更新f[i][j],同时在更新过程中不断比较更新的值与最大值,若大于最大值,就储存下来。
具体程序:
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int n,j,ans,f[1000][1000],a[1000];
int main()
{
  scanf("%d",&n);
  for (int i=1;i<=n;i++)  cin>>a[i];
  for (int i=1;i<=n;i++) f[i][i]=a[i];
  ans=0;
  for (int len=1;len<=n;len++)
    for (int i=1;i<=n-len+1;i++)
    {
      j=i+len-1;
      for (int k=i;k<=j-1;k++)
      if (f[i][k]==f[k+1][j]) f[i][j]=max(f[i][j],f[k+1][j]+1);
      if (f[i][j]>ans) ans=f[i][j];
    }
  printf("%d
",ans);
  return 0;
}
原文地址:https://www.cnblogs.com/2014nhc/p/6232623.html