luogu P3147 [USACO16OPEN]262144

题目描述

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 NN positive integers (2 \leq N\leq 262,1442≤N≤262,144), each in the range 1 \ldots 401…40. 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 NN, and the next NN lines give the sequence

of NN numbers at the start of the game.

输出格式:

Please output the largest integer Bessie can generate.

输入输出样例

输入样例#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

dp[i][j]表示从j开始合并没合并到i时的下表,由于合并必须是连续的一段区间

就可以用类似倍增更新一下是否可以合并出

#include<cstdio>
#include<algorithm>
#include<cstring>
inline int read() {
    int x=0;
    char c=getchar();
    while(c<‘0‘||c>‘9‘) c=getchar();
    while(c<=‘9‘&&c>=‘0‘)x=x*10+c-‘0‘,c=getchar();
    return x;
}
const int maxn = 277777;
int f[40+19][maxn];
int a[maxn];
int ans=0;
int main() {
    int n=read();
    for(int i=1;i<=n;++i) a[i]=read(),f[a[i]][i]=i+1;
    for(int i=1;i<=58;++i) {
        for(int j=1;j<=n;++j) {
            f[i][j]=f[i][j]|f[i-1][f[i-1][j]];
            if(f[i][j])ans=i;
        }
    }
    printf("%d\n",ans);
    return 0;
}
    
时间: 2024-11-09 03:59:36

luogu P3147 [USACO16OPEN]262144的相关文章

洛谷 P3147 [USACO16OPEN]262144

P3147 [USACO16OPEN]262144 题目描述 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.

P3147 [USACO16OPEN]262144

题目描述 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 seq

【题解】 P3147 [USACO16OPEN]262144

区间DP,题目十分类似2048的游戏,不过从二维的平面换成了数列 数据范围中 $N <= 2*10^5$ 看来$O(N^2)$肯定是无法通过了,那么设计状态N只能添加到一维上应该是类似于$O(NlogN)$的算法 再观察题目,可以提取三个关键变量,对于某个能合并成一个数的数列中的区间,有它的左边界,右边界和合成的数三个关键变量 那么设计状态就可以设$f(i,k)$表示右边界,以$i$为左边界,$k$为合成的数易得到$f(i,k+1)=f(f(i,k)+1,k)$ 枚举$k$直到最大值就完事了,$

luogu P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver解题报告

题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporarily close down his farm to save money in the meantime. The farm consists of NN barns connected with MM bidirectional paths between some pairs of

luogu P3146 [USACO16OPEN]248

基础区间的dp题 状态很容易得出:dp[i][j]表示区间i--j可以合成的最大数. 状态转移方程很显然:if (dp[i][k]==dp[k+1][j]) dp[i][j]=max(dp[i][j],dp[i][k]+1) 那么只需要先枚举结点,在以该结点为中心向两边枚举长度即可,但一次循环不能保证所有数据都正确,因为后得出的区间可能更新前得出的区间. 为了解决问题,跑多次循环 #include<iostream> #include<cstdio> #include<cma

Luogu P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

又是一个学数据结构学傻了的人 才不会承认是看到Splay,觉得可以写平衡树才进来的呢 Description: 给出一个序列,问排序后,选取两个区间,使其没有重合部分,且每个区间右端点减去左端点不大于k,求这两个区间长度之和的最大值. 前置技能:FHQ-Treap.线段树 (不会的出门百度) Solution: 看数据范围,5e4,很好,O(nlogn)完全可以水过去.那么我们可以放心的考虑FHQ了.因为要最优,所以我们要找到每一个点,在有序序列中对应的满足题目条件的区间的大小,这就可以用FHQ

luoguP3147 [USACO16OPEN]262144

这里有一种非常鬼畜的\(dp\)方法. 令\(dp[i][j]\)表示区间的最大值为$ i \(,区间的起始点为\) j $的区间长度. \[ \therefore dp[i][j]=dp[i-1][j]+dp[i-1][j+dp[i-1][j]] \] 当然前提是\(dp[i-1][j]\)和\(dp[i-1][j+dp[i-1][j]]\)能够取到. 答案是能够取到的\(dp[I][J]\)中\(I\)的最大值. 如何想到的呢?其实这是一个套路. 如果将\(dp[i][j]\)设为区间起始点

浴谷夏令营题单

这是群里一位神犇整理的,我只负责将它们做完. 一.暴力.搜索Luogu 1588 丢失的牛Luogu 1463 [SDOI2005]反素数antBzoj 1085 [SCOI2005]骑士精神Luogu 1019 单词接龙Luogu 1078 文化之旅Luogu 1312 Mayan游戏Luogu 3823 蚯蚓排队Codeforces 444B Codeforces 555DLuogu 1979 华容道 二.初等数论Poj 3292 H合成数Luogu 1890 gcd区间Luogu 1029

Luogu P2326 AKN&#39;s PPAP【按位贪心】

题目描述 "I have a pen,I have an apple.Eh,Apple-Pen!. I have a pen,I have pineapple.En,Pineapple-Pen! Apple-Pen,Pineapple-Pen.Eh,Pen-Pineapple-Apple-Pen!" Akn最近中毒于一首音乐,于是他买来了一堆苹果来学习这首音乐.Akn发现,只要边唱这首歌,边做把两个完整的苹果碰在一起的动作,两个苹果就会融合成一个新的大苹果,但是大苹果却不能再融合,因为