「TJOI2013」最长上升子序列

时间:2020-01-14
本文章向大家介绍「TJOI2013」最长上升子序列,主要包括「TJOI2013」最长上升子序列使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

「TJOI2013」最长上升子序列

传送门
这个 \(\text{DP}\) 应该都会撒:
\[dp_i = \max_{j < i,a_j < a_i}\left\{dp_j\right\} + 1\]
考虑一个性质:加入的数是严格单调递增的,所以我们每次插入一个点时,它之前的所有点都可以成为决策点,并且之前的点的 \(dp\) 值不会被更新,所以我们只需要做到插入点和询问前缀最大值即可,这个用平衡树很好做。
需要注意的是,由于平衡树的每一个节点就是序列中的点,所以在 \(\text{pushup}\) 的时候记得合并根的信息。
参考代码:

#include <algorithm>
#include <cstdlib>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while ('0' > c || c > '9') f |= c == '-', c = getchar();
    while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
    s = f ? -s : s; 
}

const int _ = 1e5 + 5;

int n, dp[_], rt, tot, pos[_], siz[_], val[_], pri[_], ch[2][_];

inline int Newnode(int v, int x)
{ return siz[++tot] = 1, dp[pos[tot] = x] = val[tot] = v, pri[tot] = rand(), tot; }

inline void pushup(int p) {
    siz[p] = siz[ch[0][p]] + siz[ch[1][p]] + 1;
    val[p] = max(dp[pos[p]], max(val[ch[0][p]], val[ch[1][p]]));
}

inline int merge(int x, int y) {
    if (!x || !y) return x + y;
    if (pri[x] > pri[y])
        return ch[1][x] = merge(ch[1][x], y), pushup(x), x;
    else
        return ch[0][y] = merge(x, ch[0][y]), pushup(y), y;
}

inline void split(int p, int k, int& x, int& y) {
    if (!p) { x = y = 0; return ; }
    if (siz[ch[0][p]] + 1 <= k)
        return x = p, split(ch[1][p], k - siz[ch[0][p]] - 1, ch[1][x], y), pushup(p);
    else
        return y = p, split(ch[0][p], k, x, ch[0][y]), pushup(p);
}

int main() {
    read(n);
    for (rg int o, a, b, i = 1; i <= n; ++i) {
        read(o), split(rt, o, a, b);
        rt = merge(a, merge(Newnode(val[a] + 1, i), b));
        printf("%d\n", val[rt]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/zsbzsb/p/12190622.html