Problem

给你n个数A1~An,每次将i插入第Ai位后,最后输出每次插入后这个数列的最长上升子序列

Solution

这道题非常的妙。首先如果新加入的这个数构成了最长上升子序列,由于在它插入之前都是比它小的数,所以就是最后这个序列这个位置的最长上升子序列。

如果不是最长的,只需要和前面那个数插入构成的最长上升子序列长度取max。

构造最后的序列长度可以用Treap维护。

Notice

插入点时,不用记录是第几个数,因为Treap新建节点的顺序就是插入的顺序。

Code

非旋转Treap

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 100000;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int point = 0, root, f[N + 5], g[N + 5], T[N + 5], now = 0;
struct node
{
int Val[N + 5], Level[N + 5], Size[N + 5], Son[2][N + 5];
inline void up(int u)
{
Size[u] = Size[Son[0][u]] + Size[Son[1][u]] + 1;
}
int Newnode(int v)
{
int u = ++point;
Val[u] = v, Level[u] = rand();
Son[0][u] = Son[1][u] = 0, Size[u] = 1;
return u;
}
int Merge(int X, int Y)
{
if (X * Y == 0) return X + Y;
if (Level[X] < Level[Y])
{
Son[1][X] = Merge(Son[1][X], Y);
up(X); return X;
}
else
{
Son[0][Y] = Merge(X, Son[0][Y]);
up(Y); return Y;
}
}
void Split(int u, int t, int &x, int &y)
{
if (!u)
{
x = y = 0;
return;
}
if (Size[Son[0][u]] < t) x = u, Split(Son[1][u], t - Size[Son[0][u]] - 1, Son[1][u], y);
else y = u, Split(Son[0][u], t, x, Son[0][u]);
up(u);
}
void Build(int l, int r)
{
int last, u, s[N + 5], top = 0;
rep(i, l, r)
{
int u = Newnode(T[i]);
last = 0;
while (top && Level[s[top]] > Level[u])
{
up(s[top]);
last = s[top];
s[top--] = 0;
}
if (top) Son[1][s[top]] = u;
Son[0][u] = last;
s[++top] = u;
}
while (top) up(s[top--]);
root = s[1];
}
int Find_rank(int v)
{
int x, y, t;
Split(root, v - 1, x, y);
t = Size[x];
root = Merge(x, y);
return t + 1;
}
int Find_num(int u, int v)
{
if (!u) return 0;
if (v <= Size[Son[0][u]]) return Find_num(Son[0][u], v);
else if (v <= Size[Son[0][u]] + 1) return u;
else return Find_num(Son[1][u], v - Size[Son[0][u]] - 1);
}
void Insert(int v)
{
int t = Newnode(v), x, y;
Split(root, v, x, y);
root = Merge(Merge(x, t), y);
}
void Out(int u)
{
if (!u) return;
Out(Son[0][u]);
T[++now] = u;
Out(Son[1][u]);
}
}Treap; int sqz()
{
int n = read();
rep(i, 1, n)
{
int x = read();
Treap.Insert(x);
}
Treap.Out(root);
g[0] = -1, f[0] = 1;
int len = 0;
rep(i, 1, n)
{
int t = lower_bound(g, g + len + 1, T[i]) - g;
f[T[i]] = t;
if (t == len + 1) g[++len] = T[i];
else g[t] = T[i];
}
rep(i, 1, n)
{
f[i] = max(f[i - 1], f[i]);
printf("%d\n", f[i]);
}
return 0;
}

旋转Treap

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 100000;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int point = 0, root, f[N + 5], g[N + 5], T[N + 5], now = 0;
struct node
{
int Val[N + 5], Level[N + 5], Size[N + 5], Son[2][N + 5];
inline void up(int u)
{
Size[u] = Size[Son[0][u]] + Size[Son[1][u]] + 1;
}
inline void Newnode(int &u, int v)
{
u = ++point;
Level[u] = rand(), Val[u] = v;
Size[u] = 1, Son[0][u] = Son[1][u] = 0;
}
inline void Lturn(int &x)
{
int y = Son[1][x]; Son[1][x] = Son[0][y], Son[0][y] = x;
Size[y] = Size[x]; up(x); x = y;
}
inline void Rturn(int &x)
{
int y = Son[0][x]; Son[0][x] = Son[1][y], Son[1][y] = x;
Size[y] = Size[x]; up(x); x = y;
} void Insert(int &u, int t)
{
if (u == 0)
{
Newnode(u, t);
return;
}
Size[u]++;
if (Size[Son[0][u]] >= t)
{
Insert(Son[0][u], t);
if (Level[Son[0][u]] < Level[u]) Rturn(u);
}
else
{
Insert(Son[1][u], t - Size[Son[0][u]] - 1);
if (Level[Son[1][u]] < Level[u]) Lturn(u);
}
}
int Find_num(int u, int t)
{
if (!u) return 0;
if (t <= Size[Son[0][u]]) return Find_num(Son[0][u], t);
else if (t <= Size[Son[0][u]] + 1) return Val[u];
else return Find_num(Son[1][u], t - Size[Son[0][u]] - 1);
}
void Out(int u)
{
if (!u) return;
Out(Son[0][u]);
T[++now] = u;
Out(Son[1][u]);
}
}Treap; int sqz()
{
int n = read();
rep(i, 1, n)
{
int x = read();
Treap.Insert(root, x);
}
Treap.Out(root);
g[0] = -1, f[0] = 1;
int len = 0;
rep(i, 1, n)
{
int t = lower_bound(g, g + len + 1, T[i]) - g;
f[T[i]] = t;
if (t == len + 1) g[++len] = T[i];
else g[t] = T[i];
}
rep(i, 1, n)
{
f[i] = max(f[i - 1], f[i]);
printf("%d\n", f[i]);
}
return 0;
}

[BZOJ3173]最长上升子序列的更多相关文章

  1. [bzoj3173]最长上升子序列_非旋转Treap

    最长上升子序列 bzoj-3173 题目大意:有1-n,n个数,第i次操作是将i加入到原有序列中制定的位置,后查询当前序列中最长上升子序列长度. 注释:1<=n<=10,000,开始序列为 ...

  2. [BZOJ3173][Tjoi2013]最长上升子序列

    [BZOJ3173][Tjoi2013]最长上升子序列 试题描述 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上 ...

  3. 【LG4309】【BZOJ3173】[TJOI2013]最长上升子序列

    [LG4309][BZOJ3173][TJOI2013]最长上升子序列 题面 洛谷 BZOJ 题解 插入操作显然用平衡树就行了 然后因为后面的插入对前面的操作无影响 就直接在插入完的序列上用树状数组求 ...

  4. 【bzoj3173】最长上升子序列

    Portal --> bzoj3173 Solution 感觉自己需要智力康复qwq 首先题目给的这个序列肯定是一个\(1-n\)的排列,并且插入的顺序是从小到大 仔细思考一下会发现如果知道了最 ...

  5. BZOJ3173 TJOI2013最长上升子序列(Treap+ZKW线段树)

    传送门 Description 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? Input ...

  6. bzoj3173[Tjoi2013]最长上升子序列 平衡树+lis

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2253  Solved: 1136[Submit][S ...

  7. bzoj千题计划316:bzoj3173: [Tjoi2013]最长上升子序列(二分+树状数组)

    https://www.lydsy.com/JudgeOnline/problem.php?id=3173 插入的数是以递增的顺序插入的 这说明如果倒过来考虑,那么从最后一个插入的开始删除,不会对以某 ...

  8. BZOJ3173:[TJOI2013]最长上升子序列(Splay)

    Description 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? Input 第一行一 ...

  9. bzoj3173: [Tjoi2013]最长上升子序列(fhqtreap)

    这题用fhqtreap可以在线. fhqtreap上维护以i结尾的最长上升子序列,数字按从小到大加入, 因为前面的数与新加入的数无关, 后面的数比新加入的数小, 所以新加入的数对原序列其他数的值没有影 ...

随机推荐

  1. 学习笔记50—多重假设检验与Bonferroni校正、FDR校正

    总结起来就三句话: (1)当同一个数据集有n次(n>=2)假设检验时,要做多重假设检验校正 (2)对于Bonferroni校正,是将p-value的cutoff除以n做校正,这样差异基因筛选的p ...

  2. MySQL学习(十五)

    索引的概念 索引是数据的目录,能快速定位数据的位置.索引提高了查询速度,降低了增删改的速度.并非加的越多越好. 一般在查询频率高的列上加,而且在重复度低的列上加效果更好.如在性别列上不用加索引,但是身 ...

  3. Spring Cloud 入门教程(一): 服务注册

    1.  什么是Spring Cloud? Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁 ...

  4. Lua和C++交互 学习记录之七:C++全局函数注册为Lua模块

    主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3  参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 1 ...

  5. 第一个Netty程序

    netty就是一个高性能的NIO框架,用于java网络编程.下面说说思路: 服务端: 开启通道.设置网络通信方式.设置端口.设置接收请求的handler.绑定通道.最后关闭 客户端: 开启通道.设置网 ...

  6. Transcranial magnetic stimulation (TMS)

    Transcranial magnetic stimulation (TMS) Effect of Transcranial Magnetic Stimulation on Free Will Tra ...

  7. p1473 Zero Sum

    搜索,最后判断一下是否结果为0就行. #include <iostream> #include <cstdio> #include <cmath> #include ...

  8. 宽度优先搜索BFS(Breadth-First-Search)

    Breadth-First-Search 1. 与DFS的异同 相同点:搜索所有可能的状态. 不同点:搜索顺序. 2. BFS总是先搜索距离初始状态近的状态,它是按照:开始状态->只需一次转移就 ...

  9. zsh切换bash bash切换zsh

    切换bash(需要sudo) chsh -s /bin/bash 切换zsh(不需要sudo) chsh -s /bin/zsh 注意:如果输入命令和密码后提示:no change made. 请加上 ...

  10. vue项目 sockjs-node一直报错问题

    vue3下 vue.config.js中 devServer: { host: '0.0.0.0', port: 8080, proxy: { '/': { target: 'http://127.0 ...