区间dp--洛谷P3147 [USACO16OPEN] 262144 P

4 篇文章 0 订阅
3 篇文章 0 订阅
本文介绍了如何使用动态规划方法帮助Bessie在游戏中通过合并数字最大化最终序列中最大值,涉及状态转移和区间选择策略。
摘要由CSDN通过智能技术生成

[USACO16OPEN] 262144 P

洛谷链接

题目描述

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 N N N positive integers ( 2 ≤ N ≤ 262 , 144 2 \leq N\leq 262,144 2N262,144), each in the range 1 … 40 1 \ldots 40 140. 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来求最大值。

输入格式

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

样例输入 #1

4
1
1
1
2

样例输出 #1

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.

题目分析

此题目较难考虑
设状态转移方程为 f [ i , j ] f[i,j] f[i,j]表示能够合成 i i i且左端点为 j j j
右端点位置注意此时区间为左开右闭
则状态转移方程为
f [ i , j ] = f [ i − 1 ] [ f [ i − 1 ] [ j ] ] f[i,j]=f[i-1][f[i-1][j]] f[i,j]=f[i1][f[i1][j]]
此时表示的是合成 i i i这个数需要两个 i − 1 i-1 i1来合成,其中一个 i − 1 i-1 i1的合成区间的右端点即为 f [ i − 1 , j ] f[i-1,j] f[i1,j]
用此右端点作为左端点再合成一个 i − 1 i-1 i1,即可得到 i i i
即合成 i i i的右端点即为合成第二个 i − 1 i-1 i1的右端点
递推即可求解答案
细节考虑

  1. 为何要设置左闭右开区间,而不是设置左闭右闭区间
    如果设置左闭右闭区间:
    那么状态转移方程为
    f [ i , j ] = f [ i − 1 ] [ f [ i − 1 , j ] + 1 ] f[i,j]=f[i-1][f[i-1,j]+1] f[i,j]=f[i1][f[i1,j]+1]此时就会出现不合法问题
    比如我们当前有不合法状态, f [ i − 1 , j ] f[i-1,j] f[i1,j]应当为0
    如果是左闭右闭区间,那么会将其 + 1 +1 +1,此时可能会由其他合法状态转移过来,导致答案不正确
    换句话说
    如果我们设置左闭右开区间,那么可以防止不合法状态,转移,因为不合法状态必定为0
  2. 为什么数值是58,根据题目范围可以得出区间长度计算可以得到最大数值

code
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=66,M=270000;
int f[N][M];
int n;
int ans=0;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int k;
        cin>>k;
        f[k][i]=i+1;//由于是左闭右开区间,所以设置为i+1
    }
    for(int i=2;i<=58;i++)//枚举状态转移方程
        for(int j=1;j<=n;j++)
        {
            if(!f[i][j]){
                f[i][j]=f[i-1][f[i-1][j]];
            }
           if(f[i][j]){//此状态存在右端点,说明状态合法,此时更新答案
            ans=i;
           }
        }
        cout<<ans<<endl;
        return 0;
}

  • 32
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值