【USACO】2016 Open 262144

15 篇文章 0 订阅

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 Format

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 Format

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.


  • 分析

刚开始做的时候,觉得应该是道贪心题(看起来挺像的),然后开始不停地想,不停地用什么队列、栈、单调队列、单调栈去做,结果并没有什么用。听说正解有一种就是贪心,果然还是我太菜了,到现在也没懂,在这里暂时不写,什么时候懂了再写。
下面讲讲另一种解法:动规。我们知道从两个数要合成必须靠在一起,当然他们也有可能是有若干个小数合成的。那我们用F[i][j] 记录从j开始合成出i的长度为多少(比如1 3 4 3 2 1 1,那F[5][3]=5,注意是合成而不是这段区间的最大值),那如果从j+F[i][j] 开始也能合成出i,也就是F[i][j+F[i][j]]有值,那就代表我们能从j开始一直到F[i][j+F[i][j]],把他们合成i+1,即F[i+1][j]=F[i][j]+F[i][j+F[i][j]]。(是不是看起来很像倍增的方程,其实倍增也是动规的一种)


#include <cstdio> 
using namespace std;
int n,ans=0,x,f[60][262150];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        if(x>ans) ans=x;
        f[x][i]=1;
    }
    for (int i=1;i<=59;i++){
        for(int j=1;j<=n;j++)
            if (f[i][j]){
                if (j+f[i][j]>n) continue;
                if (f[i][j+f[i][j]]){
                    f[i+1][j]=f[i][j]+f[i][j+f[i][j]];
                    if (i+1>ans) ans=i+1;
                }
            }
    }
    printf("%d\n",ans);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值