[BZOJ 3173] [TJOI 2013] 最长上升子序列(fhq treap)

题面

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

分析

这题有几个重要性质:

第一个性质是,插入的数的大小是递增的。我们插入第i个数后的答案,为以当前序列中的数为结尾的LIS长度的最大值,而根据当前序列中的数都在[1,i]内,们可以维护以值v为结尾的LIS长度len[i],那么第i次插入后的答案就是\(max(len[j]) (j \in [1,i])\)

第二个性质是,后插入的数不会影响以前面插入的数为结尾的LIS长度。这句话有点抽象,我们来举一个例子。插入完两个数后的序列是{1,2},以2结尾的LIS长度为2。然后我们在2前面插入3,序列变成了{1,3,2},因为3>2,它不会影响以2结尾的LIS长度,因此以2结尾的LIS长度仍为2。如果插入在2后面,显然新的LIS结尾不可能是2,对以2结尾的LIS长度没有影响。

因此,我们只要用一个数据结构处理出插入n次后的最终序列,对这个序列跑一次LIS,求出以v结尾的序列长度,然后前缀max一下求出答案。这里我选择了fhq treap来维护序列,然后用模板的\(O(n \log n)\)求LIS算法。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define maxn 100000
using namespace std;
struct fhq_treap{
#define lson(x) tree[x].ls
#define rson(x) tree[x].rs
struct node{
int ls;
int rs;
int val;
int sz;
int dat;
}tree[maxn+5];
int root;
int ptr;
int New(int val){
ptr++;
tree[ptr].val=val;
tree[ptr].dat=rand();
tree[ptr].sz=1;
return ptr;
}
void push_up(int x){
tree[x].sz=tree[lson(x)].sz+tree[rson(x)].sz+1;
}
int merge(int x,int y){
if(!x||!y) return x+y;
if(tree[x].dat<tree[y].dat){
tree[y].ls=merge(x,tree[y].ls);
push_up(y);
return y;
}else{
tree[x].rs=merge(tree[x].rs,y);
push_up(x);
return x;
}
}
void split(int now,int k,int &x,int &y){
if(now==0){
x=y=0;
return;
}
if(k<=tree[lson(now)].sz){
y=now;
split(tree[now].ls,k,x,tree[y].ls);
}else{
x=now;
split(tree[now].rs,k-tree[lson(now)].sz-1,tree[x].rs,y);
}
push_up(now);
}
void insert(int val,int pos){
int x,y;
split(root,pos-1,x,y);
root=merge(merge(x,New(val)),y);
}
void print(int x,int *out,int &sz){
if(x==0) return;
print(lson(x),out,sz);
out[++sz]=tree[x].val;
print(rson(x),out,sz);
}
}T; int n;
int sz=0;
int a[maxn+5]; int top=0;
int s[maxn+5];
int len[maxn+5];//以值i为结尾的LIS长度
void get_lis(){
for(int i=1;i<=n;i++){
if(a[i]>s[top]){
s[++top]=a[i];
len[a[i]]=top;
}else{
int tmp=lower_bound(s+1,s+1+top,a[i])-s;
s[tmp]=a[i];
len[a[i]]=tmp;
}
}
}
int main(){
int x;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&x);
x++;
T.insert(i,x);
}
T.print(T.root,a,sz);
// for(int i=1;i<=sz;i++) printf("%d ",a[i]);
get_lis();
int ans=0;
for(int i=1;i<=n;i++){
ans=max(ans,len[i]);
printf("%d\n",ans);
//第i个数插入的时候,序列里只有1~i的数,把以它们为结尾的lis长度取max即可
}
}

[BZOJ 3173] [TJOI 2013] 最长上升子序列(fhq treap)的更多相关文章

  1. 【bzoj 3173】[Tjoi2013]最长上升子序列

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

  2. [TJOI]2013 最长上升子序列

    这个题据说是Splay,或者说是平衡树的模板题,但是我还是不会做--唉-- \(\color{red}{Description}\) 给定一个序列,初始为空.现在我们将\(1\)到\(N\)的数字插入 ...

  3. 2021.12.07 [TJOI2013]最长上升子序列(Treap+DP)

    2021.12.07 [TJOI2013]最长上升子序列(Treap+DP) https://www.luogu.com.cn/problem/P4309 题意: 给定一个序列,初始为空.现在我们将1 ...

  4. Bzoj 3173: [Tjoi2013]最长上升子序列 平衡树,Treap,二分,树的序遍历

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1183  Solved: 610[Submit][St ...

  5. bzoj 3173 [Tjoi2013]最长上升子序列 (treap模拟+lis)

    [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2213  Solved: 1119[Submit][Status] ...

  6. BZOJ 3170: [Tjoi 2013]松鼠聚会 切比雪夫距离

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  7. BZOJ 3170: [Tjoi 2013]松鼠聚会( sort )

    题目的距离为max(|x1-x2|, |y1-y2|) (切比雪夫距离). 切比雪夫距离(x, y)->曼哈顿距离((x+y)/2, (x-y)/2) (曼哈顿(x, y)->切比雪夫(x ...

  8. Bzoj 3170[Tjoi 2013]松鼠聚会 曼哈顿距离与切比雪夫距离

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1318  Solved: 664[Submit][Stat ...

  9. 【bzoj3173】【Tjoi2013】【最长上升子序列】treap+dp二分优化

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=61560361 向大(hei)佬(e)实力学(di ...

随机推荐

  1. namenode和datanode的高可用性和故障处理

    一.Hadoop单点故障问题如何解决 Hadoop 1.0内核主要由两个分支组成:MapReduce和HDFS,众所周知,这两个系统的设计缺陷是单点故障,即MR的JobTracker和HDFS的Nam ...

  2. 查看jar包内容

    查看jar包内容 查看jar包内容的基本命令: jar tf jar-file 参数解释: The t option indicates that you want to view the table ...

  3. JAVA的深浅拷备

    package com.jd.ng.shiro.testFactory; import java.io.*; /** * @author wangzhilei * @Author: husToy.Wa ...

  4. SpringBoot配置自定义美化Swagger2

    1.添加maven依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springf ...

  5. 移动web开发之像素和DPR详解

    前话: 像素在web开发中几乎天天用到,但到底什么是像素,移动端和桌面端的像素有区别吗,缩放对像素有影响吗,视网膜屏幕和像素有什么关系?关于这些问题,可能就不清楚了.本文将介绍关于像素的相关知识 什么 ...

  6. Rosetta Stone 不在C盘安装步骤

    本文出自:http://www.cnblogs.com/2186009311CFF/p/7500637.html Rosetta Stone默认安装在C盘的,很不好,故找到次解决方案: 总体就是移动文 ...

  7. vue的生产环境dependencies 和开发环境devDependencies,二者的理解和区别

  8. react native 实现TODO APP

    前端有一个todo app非常适合入门练手 react-native 实现todo app:https://github.com/nwgdegitHub/TODO_RN.git

  9. 在 Postman 中报错:Self-signed SSL certificates are being blocked 的分析与解决

    http://www.shuijingwanwq.com/2019/02/18/3171/

  10. getCurrentPages

    解释:getCurrentPages 全局函数用于获取当前页面栈的实例,以数组形式按栈的顺序给出,第一个元素为首页,最后一个元素为当前页面. 示例: // index.jsPage({ onShow( ...