P3147 [USACO16OPEN]262144 P

题目链接
题目描述:# [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.
思路:
第一时间想区间dp,一看范围直接G了。这题维护的信息很有意思
f [ i ] [ j ] f[i][j] f[i][j] 表示合成的数字为 i i i ,并且这个区间的左端点为 j j j 时的右端点。

需要连续两段的值都为 i − 1 i - 1 i1 时才能合成 i i i
如下图,那么此时的状态转移方程为:
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]]
f [ i − 1 ] [ j ] f[i - 1][j] f[i1][j] 表示左端点为 a a a, 值为 i − 1 i - 1 i1时的右端点,即为下图中点 b b b
那么以 b b b 为左端点,值为 i − 1 i - 1 i1 的点就可以表示为 f [ i − 1 ] [ f [ i − 1 ] [ j ] ] f[i - 1][f[i - 1][j]] f[i1][f[i1][j]],即右端点,等于 f [ i ] [ j ] f[i][j] f[i][j]
在这里插入图片描述
初始化:
初始化时不能让 f [ i ] [ j ] = j f[i][j] = j f[i][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]] 时,会一直让 f [ i ] [ j ] = j f[i][j] = j f[i][j]=j,比如:
对于样例的: 1 1 1 1 1 1 1 1 1 2 2 2 我们将 f [ 1 ] [ 2 ] = 2 f[1][2] = 2 f[1][2]=2
f [ 2 ] [ 2 ] = f [ 1 ] [ f [ 1 ] [ 2 ] ] = f [ 1 ] [ 2 ] = 2 f[2][2] = f[1][f[1][2]] = f[1][2] = 2 f[2][2]=f[1][f[1][2]]=f[1][2]=2,右端点完全没有被改变,所以我们初始化时将所有区间右端点全部 + 1 1 1 ,最后更新出来的答案也只是多 1 1 1 的,即上面应该更新为: f [ 1 ] [ 2 ] = 3 f[1][2] = 3 f[1][2]=3 f [ 1 ] [ 3 ] = 4 f[1][3] = 4 f[1][3]=4
f [ 2 ] [ 2 ] = f [ 1 ] [ f [ 1 ] [ 2 ] ] = f [ 1 ] [ 3 ] = 4 f[2][2] = f[1][f[1][2]] = f[1][3] = 4 f[2][2]=f[1][f[1][2]]=f[1][3]=4 也只是比原来多 1 1 1,可以发现最终答案不会被影响
代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> PII;
const int N = 3e5 + 10;
int f[60][N];

int main() {
   ios::sync_with_stdio(false); 
   cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
   freopen("D:/Cpp/program/Test.in", "r", stdin);
   freopen("D:/Cpp/program/Test.out", "w", stdout);
#endif
    int n; cin >> n;
    vector<int> a(n + 1);
    for(int i = 1; i <= n; i ++) {
        int x; cin >> x;
        f[x][i] = i + 1;
    }
    int ans = 0;
    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 << '\n';
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值