题目链接

题目

题目描述

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,但状态比较巧妙。

设 \(f[i][j]\) 表示以 \(j\) 为左端点合并出数字 \(i\) 的右端点位置,代表这个区间 \([j,f[i][j])\) 能合并出一个 \(i\) 。

这里的转移方程用了个倍增的思想,即跳跃两个点。 \(f[i-1][j]\) 表示从 \(j\) 开始合并出 \(i-1\) 的右端点位置。因为要两个 \(i-1\) ,所以若 \(f[i-1][j]\) 存在,则从 \(j\) 开始到 \(f[i-1][j]\) 能合并一个 \(i-1\) ,接下来若 \(f[i-1][f[i-1][j]]\) 存在,则从 \(f[i-1][j]\) 到 \(f[i-1][f[i-1][j]]\) 合并成第二个 \(i-1\) ,于是有转移方程:

\[f[i][j]\ |=\ f[i-1][f[i-1][j]]
\]

当然如果 \(f[i-1][j]\) 或者 \(f[i-1][f[i-1][j]]\) 为 \(0\) 即不存在,是不会改变 \(f[i][j]\) 的。同时不可能存在 \(f[i][j]\) 和 \(f[i-1][j]\) 同时存在的情况,不必担心或运算改变本来的值。

初始状态是 \(f[x][i] = i+1\) ,表示 \([i,i+1)\) 能合成一个 \(x\) 。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#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;
}

NC24438 [USACO 2016 Ope P]262144的更多相关文章

  1. NC24017 [USACO 2016 Jan S]Angry Cows

    NC24017 [USACO 2016 Jan S]Angry Cows 题目 题目描述 Bessie the cow has designed what she thinks will be the ...

  2. NC25136 [USACO 2006 Ope B]Cows on a Leash

    NC25136 [USACO 2006 Ope B]Cows on a Leash 题目 题目描述 给定如图所示的若干个长条.你可以在某一行的任意两个数之间作一条竖线,从而把这个长条切开,并可能切开其 ...

  3. USACO 2016 US Open Contest, Gold解题报告

    1.Splitting the Field http://usaco.org/index.php?page=viewproblem2&cpid=645 给二维坐标系中的n个点,求ans=用一个 ...

  4. USACO 2016 February Contest, Gold解题报告

    1.Circular Barn   http://www.usaco.org/index.php?page=viewproblem2&cpid=621 贪心 #include <cstd ...

  5. USACO 2016 January Contest, Gold解题报告

    1.Angry Cows http://www.usaco.org/index.php?page=viewproblem2&cpid=597 dp题+vector数组运用 将从左向右与从右向左 ...

  6. usaco 2016 Feb 负载平衡

    题目大意:平面上一堆点,用两条平行于坐标轴的直线将其分为四部分,使得点数最多的一部分最少 第一维枚举,第二维三分,点集用两棵树状数组维护 #include<bits/stdc++.h> # ...

  7. [USACO 2016 Dec Gold] Tutorial

    Link: 传送门 A: 贪心从小到大插入,用并查集维护连通性 #include <bits/stdc++.h> using namespace std; #define X first ...

  8. COGS130. [USACO Mar08] 游荡的奶牛[DP]

    130. [USACO Mar08] 游荡的奶牛 ★☆   输入文件:ctravel.in   输出文件:ctravel.out   简单对比时间限制:1 s   内存限制:128 MB 奶牛们在被划 ...

  9. COGS182 [USACO Jan07] 均衡队形[RMQ]

    182. [USACO Jan07] 均衡队形 ★★   输入文件:lineup.in   输出文件:lineup.out   简单对比时间限制:4 s   内存限制:128 MB 题目描述 农夫约翰 ...

  10. 2016 Multi-University Training Contest 1 F.PowMod

    PowMod Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Su ...

随机推荐

  1. C++ list容器

    一.前言 list容器,又称为双向链表容器,即该容器的底层是以双向链表的形式实现的,因此list容器中的元素可以分散存储在内存空间里,而不是必须存储在一整块连续的内存空间中. list容器中各个元素的 ...

  2. 有n个整数,使前面各数顺序向后m个位置,最后m个数变成最前面m个数,见图 8.43。写一函数实现以上功能在主函数中输入个整数和输出调整后的n个数。

    4,有n个整数,使前面各数顺序向后m个位置,最后m个数变成最前面m个数,见图 8.43.写一函数实现以上功能在主函数中输入个整数和输出调整后的n个数. 我的代码: 1.使用双向链表 void Move ...

  3. 【ThreadX-GUIX】Azure RTOS GUIX和Azure RTOS GUIX Studio概述

    Azure GUIX嵌入式GUI是Microsoft的高级工业级GUI解决方案,专门针对深度嵌入式,实时和IoT应用程序而设计.Microsoft还提供了名为Azure RTOS GUIX Studi ...

  4. web - 解决 formdata 打印空对象

    获取单个值可以使用formData对象.get();而直接打印是看不到的.因为外界访问不到,你使用append方法后,对应的键值对就已经添加到表单里面了,你在控制台看到的是FormData原型,存储的 ...

  5. [转帖]银河麒麟v10下载(服务器版 桌面版) - 2023-11-14更新

    银河麒麟v10下载(服务器版 桌面版) - 2023-11-14更新 如需转载请标明出处:[http://blog.csdn.net/itas109] 文章目录 银河麒麟v10下载(服务器版 桌面版) ...

  6. [转帖]Jmeter学习笔记(十九)——后置处理器之正则表达式的使用

    https://www.cnblogs.com/pachongshangdexuebi/p/11733005.html 一.正则表达式提取器的作用 允许用户从服务器的响应中通过使用perl的正则表达式 ...

  7. [转帖]IPv6地址解析库,窥探IPv6地址中包含的信息

    https://zhuanlan.zhihu.com/p/479028720 大家好,我是明说网络的小明同学. 今天和大家介绍一个IPv6 地址解析库IPv6 address Parser :http ...

  8. [转帖]手把手教你在QEMU上运行RISC-V Linux

    https://kernel.0voice.com/forum.php?mod=viewthread&tid=3080   嵌入式Linux内核 发布于 2023-3-15 14:44:37  ...

  9. Find 查找并且展示最近24小时内创建的文件信息

    1. 命令为: find /gscloud/tools/patchinstall/patchfiles/ -maxdepth 1 -mtime 1 |cut -c40- >/deploy/pat ...

  10. sed 反斜线换成正斜线

    命令为: sed -i "s:\\\:\/:g" yourfiles