【题解】 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$直到最大值就完事了,$k < 40+logN$ 设置一个常数为边界

注意一下特殊处理,如果不能合成任何数大于原数列最大值,那么输出原数列最大值,而非合成数的最大值

```
//2019/7/14->Riko->AtNCU->luogu3147
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long

bool digit (int x) { return (x <= ‘9‘ and x >= ‘0‘);}
int in () {
    int x = 0, ch = getchar();
    while (!digit(ch)) ch = getchar();
    while (digit(ch)) x = x*10+ch-‘0‘, ch = getchar();
    return x;
}
void smax (int& x, int y) { if (x < y) x = y;}
void print (int x) { printf("debug:%d\n", x);}

const int TOP = 303000;
int n, ans;
int f[TOP][64], k[TOP];

void work () {
    for (int num = 1; num <= 64; ++num) {
        for (int i = 1; i < n; ++i) {
            //printf("i:%d num:%d f[i][num]:%d\n", i, num, f[i][num]);
            if (f[i][num] and f[f[i][num]+1][num]) {
                f[i][num+1] = f[f[i][num]+1][num];
                smax(ans, num+1);
            }
        }
    }
    printf("%d", ans);
}
void prepare () {
    n = in();
    for (int i = 1; i <= n; ++i) {
        k[i] = in();
        smax(ans, k[i]);
        f[i][k[i]] = i;
    }
} int main () { prepare(); work();}
```

原文地址:https://www.cnblogs.com/NHDR233/p/11246703.html

时间: 2024-08-30 17:06:58

【题解】 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.

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 seq

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

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 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

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

题解小合集——第一弹

(转载于我的洛谷博客) 索引: 第一题:P2552 团体操队形 第二题:P3146 248 第三题:P3147 262144 第四题:P1972 花花的项链 第五题:P1484 种树 第六题:P5132 Cozy Glow之拯救小马国 第七题:P1198 最大数 第八题:P2023 维护序列 第九题:P1967 货车运输 第十题:P1313 计算系数 第一题:P2552 团体操队形 题解思路:大模拟,仔细点就好 这道题以一个点需要注意--就是梅花桩队形的奇数排列和偶数排列的规律是略有不同的 奇数

10-2国庆节第五场模拟赛题解

T1 seq: 序列2 (seq) Description 给定个长度为 n 的数列 {a},初始时数列中每个元素 a_i 都不大于 40.你可以在其上进行若干次操作.在一次操作中,你会选出相邻且相等的两个元素,并把他们合并成一个元素,新的元素值为 \((旧元素值+1)\). 请你找出,怎样的一系列操作可以让数列中的最大值变得尽可能地大?这个最大值是多少? Input 输入文件第一行一个正整数 n,表示数列的长度. 接下来一行 n 个不大于 40 的正整数,表示这个数列 {a }. Output

题解:序列 (Standard IO)

题解 序列 (Standard IO) Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed Limits Description Fiugou想要在一个长度为N的序列A中找到不同位置的三个数,以这三个数为三边长来构成一个三角形.但是它希望在满足条件下,这三个数的位置尽量靠前.具体地,设这三个数的为Ai,Aj,Ak(i<j<k), Fiugou希望k尽量小:当k相等时,满足j尽量小:当k,j均相等时,满足i尽量小.但是这个序列中的数可

洛谷 P1079 Vigen&#232;re 密码 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=1079 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南北战争中为 南军所广泛使用. 在密码学中,我们称需要加密的信息为明文,用 M 表示:称加密后的信息为密文,用 C 表示:而密钥是一种