bzoj 3173 最长上升子序列
Written with StackEdit.
Description
给定一个序列,初始为空。现在我们将\(1\)到\(N\)的数字插入到序列中,每次将一个数字插入到一个特定的位置。每插入一个数字,我们都想知道此时最长上升子序列长度是多少?
Input
第一行一个整数\(N\),表示我们要将\(1\)到\(N\)插入序列中,接下是\(N\)个数字,第\(k\)个数字\(X_k\),表示我们将\(k\)插入到位置\(X_k(0\leq X_k\leq k-1,1\leq k\leq N)\),
Output
\(N\)行,第\(i\)行表示\(i\)插入\(X_i\)位置后序列的最长上升子序列的长度是多少。
Sample Input
3
0 0 2
Sample Output
1
1
2
HINT
\(100\%\)的数据 \(n\leq100000\).
Solution
- 如果我们已经得到了最后的序列,我们可以用\(O(nlogn)\)的算法计算出\(LIS\),同时维护\(ans[i]\),表示以\(i\)作为结尾的上升子序列的最大长度.
- 再令\(g[i]\)表示最终要输出的答案,即插入\(i\)后的\(LIS\)长度.
- 因为整个序列是从小到大插入的,所以\(g[i]=max_{j=1}^{i}ans[j].\)
- 使用前缀和优化一下即可.
- 维护元素的插入可以写一颗平衡树.
#include<bits/stdc++.h>
using namespace std;
typedef long long LoveLive;
inline int read()
{
int out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
{
fh=-1;
jp=getchar();
}
while (jp>='0'&&jp<='9')
{
out=out*10+jp-'0';
jp=getchar();
}
return out*fh;
}
const int MAXN=1e5+10;
int a[MAXN],qlen=0;
int n;
struct FhqTreap
{
int x,y;
struct node
{
int lson,rson,siz,weight,key;
} treap[MAXN];
int idx,root;
FhqTreap()
{
x=0,y=0;
idx=0;
root=0;
treap[0].key=0;
treap[0].lson=treap[0].rson=0;
treap[0].weight=0;
treap[0].siz=0;
}
#define rt treap[o]
#define ls treap[treap[o].lson]
#define rs treap[treap[o].rson]
inline int newnode(int key)
{
int o=++idx;
rt.lson=rt.rson=0;
rt.siz=1;
rt.weight=rand();
rt.key=key;
return o;
}
inline void pushup(int o)
{
rt.siz=ls.siz+rs.siz+1;
}
int merge(int x,int y)
{
if(!x || !y)
return x+y;
if(treap[x].weight<treap[y].weight)
{
treap[x].rson=merge(treap[x].rson,y);
pushup(x);
return x;
}
else
{
treap[y].lson=merge(x,treap[y].lson);
pushup(y);
return y;
}
}
void split(int &x,int &y,int k,int o)
{
if(!o)
x=y=0;
else
{
if(k<=ls.siz)
{
y=o;
split(x,rt.lson,k,rt.lson);
}
else
{
x=o;
split(rt.rson,y,k-ls.siz-1,rt.rson);
}
pushup(o);
}
}
void ins(int key,int pos)
{
split(x,y,pos,root);
y=merge(newnode(key),y);
root=merge(x,y);
}
void dfs(int o)
{
if(!o)
return;
dfs(rt.lson);
a[++qlen]=rt.key;
dfs(rt.rson);
}
void getseq()
{
dfs(root);
}
}T;
#define inf 0x7fffffff
int f[MAXN],ans[MAXN];
int main()
{
srand(19260817);
n=read();
for(int i=1;i<=n;++i)
{
int pos=read();
T.ins(i,pos);
}
T.getseq();
memset(f,0x7f,sizeof f);
f[0]=-inf;
for(int i=1;i<=n;++i)
{
int t=upper_bound(f,f+n+1,a[i])-f;
f[t]=a[i];
ans[a[i]]=t;
}
for(int i=1;i<=n;++i)
printf("%d\n",ans[i]=max(ans[i-1],ans[i]));
puts("");
return 0;
}
bzoj 3173 最长上升子序列的更多相关文章
- BZOJ 3173 最长上升子序列(树状数组+二分+线段树)
给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? 由于序列是顺序插入的,所以当前插入的数字对之 ...
- BZOJ.3591.最长上升子序列(状压DP)
BZOJ 题意:给出\(1\sim n\)的一个排列的一个最长上升子序列,求原排列可能的种类数. \(n\leq 15\). \(n\)很小,参照HDU 4352这道题,我们直接把求\(LIS\)时的 ...
- BZOJ 2423 最长公共子序列
Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0, ...
- 【刷题】BZOJ 3591 最长上升子序列
Description 给出1~n的一个排列的一个最长上升子序列,求原排列可能的种类数. Input 第一行一个整数n. 第二行一个整数k,表示最长上升子序列的长度. 第三行k个整数,表示这个最长上升 ...
- BZOJ 5427: 最长上升子序列
$f[i] 表示长度为i的最长上升子序列的最后一位的最小值是多少$ 对于普通的$LIS我们可以二分确定位置去更新$ 再来考虑对于这个,如果有某一位没有确定的话 那么这一位是可以随便取的,也就是说,所有 ...
- BZOJ 5161: 最长上升子序列 状压dp+查分
好神啊 ~ 打表程序: #include <cstdio> #include <cstring> #include <algorithm> #define N 14 ...
- [BZOJ 3173] [TJOI 2013] 最长上升子序列(fhq treap)
[BZOJ 3173] [TJOI 2013] 最长上升子序列(fhq treap) 题面 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数 ...
- BZOJ 3173: [Tjoi2013]最长上升子序列
3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1524 Solved: 797[Submit][St ...
- BZOJ 3173 [Tjoi2013] 最长上升子序列 解题报告
这个题感觉比较简单,但却比较容易想残.. 我不会用树状数组求这个原排列,于是我只好用线段树...毕竟 Gromah 果弱马. 我们可以直接依次求出原排列的元素,每次找到最小并且最靠右的那个元素,假设这 ...
随机推荐
- Python面试题之Python对象反射、类反射、模块反射
python面向对象中的反射:通过字符串的形式操作对象相关的属性.python中的一切事物都是对象(都可以使用反射) 一.getattr 对象获取 class Manager: role = &quo ...
- jQuery 3D垂直多级菜单
在线演示 本地下载
- socket IPC(本地套接字 domain)
1.简介 socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是UNIX Domain Socket.虽然网络socket也可用于同一台主机的进程间通讯(通 ...
- windows技巧--一次关闭所有资源管理器目录,文件夹目录
每天开机工作一段时间以后,你可能会和我一样,打开了很多的文件目录,于是一个一个的点窗口关闭.于是想有没有一次关闭所有目录的办法~~咚咚咚,经过一番寻觅,下面是我找到的办法 新建bat文件 close_ ...
- jvm-内存区域与内存溢出异常
内存区域与内存溢出异常 参考: http://www.cnblogs.com/ityouknow/p/5610232.html 所有的Java开发人员可能会遇到这样的困惑?我该为堆内存设置多大空间呢? ...
- NSwag在asp.net web api中的使用,基于Global.asax
https://github.com/NSwag/NSwag/wiki/OwinGlobalAsax This page explains how to use the NSwag OWIN midd ...
- HttpStatusCode
https://docs.microsoft.com/en-us/dotnet/api/system.net.httpstatuscode?view=netframework-4.7.2 422 Un ...
- LeetCode——Find the Difference
LeetCode--Find the Difference Question Given two strings s and t which consist of only lowercase let ...
- 初入Spring-boot(二)
一.入口类 Spring boot通常有一个名为*Application的入口类,入口类里有一个main方法,这个main方法其实就是一个标准的java应用的入口方法.在main方法中使用Spring ...
- scala学习手记4 - Java基本类型对应的scala类
在Java中变量类型分为两大类:基本类型和引用类型.虽然在JDK1.5以后引入了自动装箱和自动拆箱机制,大大减少了我们在直接类型和引用类型之间的纠结,但仍有一些我们不得不考虑的问题.比如我在工作遇到的 ...