NC24438 [USACO 2016 Ope P]262144
题目
题目描述
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!
输入描述
The first line of input contains N, and the next N lines give the sequence of N numbers at the start of the game.
输出描述
Please output the largest integer Bessie can generate.
示例1
输入
4 1 1 1 2
输出
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,倍增。
显然是dp,但状态比较巧妙。
设 表示以 为左端点合并出数字 的右端点位置,代表这个区间 能合并出一个 。
这里的转移方程用了个倍增的思想,即跳跃两个点。 表示从 开始合并出 的右端点位置。因为要两个 ,所以若 存在,则从 开始到 能合并一个 ,接下来若 存在,则从 到 合并成第二个 ,于是有转移方程:
当然如果 或者 为 即不存在,是不会改变 的。同时不可能存在 和 同时存在的情况,不必担心或运算改变本来的值。
初始状态是 ,表示 能合成一个 。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> using namespace std; int f[60][262144 + 7];///表示数字i在j位置的下一个数字的位置(因为两个数字合并成一个,所以新数字的下一个要跳过一个数字) ///而且数字合并有很多可能,比如 1 1 1 2可以 2 1 2;1 2 2,因此对于这种记录方法可以使得数字的跳转独立,形成不同的链 int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 1, x;i <= n;i++) cin >> x, f[x][i] = i + 1; int ans = 1; for (int i = 2;i <= 58;i++) { for (int j = 1;j <= n;j++) { f[i][j] |= f[i - 1][f[i - 1][j]]; ///数字向左合并,因为链是向右的 ///表示如果数字i-1在j位置存在指向下一个位置的数字也存在,则这个数字指向下下个数字位置 ///或运算好处是如果i,j本身存在则i-1,j不可能存在最终i-1,(i-1,j)也是不存在的不会改变i,j ///如果i,j不存在则为0,就会修改成结果 if (f[i][j]) ans = i; } } cout << ans << '\n'; return 0; }
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/16577643.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧