bzoj4580: [Usaco2016 Open]248(区间dp)

4580: [Usaco2016 Open]248

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 255  Solved: 204
[Submit][Status][Discuss]

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!
给你n个数,每次可以将两个相邻的相同数x合并成x+1,求最大能合出多少
 

 

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.

HINT

 

Source

Gold 鸣谢duan2提供译文

 

/*
f[i][j]表示合并这段区间最大值
枚举中间点k,如果f[i][k]==f[k+1][j]且不等于0 就能转移。 
*/
#include<bits/stdc++.h>

#define N 250

using namespace std;
int n,ans,f[N][N];

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++)
    {
        scanf("%d",&f[i][i]);
        ans=max(ans,f[i][i]);
    }
    for (int i=n-1; i>=1; i--)
        for (int j=i+1; j<=n; j++)
        {
            for (int k=i; k<j; k++)
                if (f[i][k]==f[k+1][j] && f[i][k]!=0)
                    f[i][j]=max(f[i][j],f[i][k]+1);
            ans=max(ans,f[i][j]);
        }
    printf("%d",ans);
    return 0;
}

 

/*
题解里这个状态就比较神奇了。 
f[i][j]表示到第i个数,得到数值为j,向右合并的最右端点。
方程f[i][j]=f[f[i][j-1]][j-1];类似倍增。
j比较小,因为两个合并之后得到的数只大1,所以可以跑过去。
*/ 
#include<bits/stdc++.h>

#define N 270000

using namespace std;
int n,m,ans;
int f[N][66],a[N];
int main()
{
    freopen("ly.in","r",stdin);
    scanf("%d",&n);
    for (int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        f[i][a[i]]=i+1;
    }
    for (int j=2; j<=60; j++)
        for (int i=1; i<=n; i++)
        {
            if (f[i][j]==0) f[i][j]=f[f[i][j-1]][j-1];
            if (f[i][j]>0) ans=max(ans,j);
        }
    printf("%d\n",ans);
    return 0;
}

 

posted @ 2018-10-31 15:03  安月冷  阅读(205)  评论(0编辑  收藏  举报