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 果弱马. 我们可以直接依次求出原排列的元素,每次找到最小并且最靠右的那个元素,假设这 ...
随机推荐
- Hadoop创始人Doug Cutting寄语2017:五种让开源项目成功的方法
原文链接:http://www.infoq.com/cn/news/2017/01/Hadoop-2017-5-open-source?utm_source=tuicool&utm_mediu ...
- day1 python判断输入的密码是否正确
_username = 'leon' _password = 'zyl' username = input("username:") password = input(" ...
- $微信小程序开发实践点滴——接入Bmob后端云
Bmob后端云官网:http://www.bmob.cn/ 微信公众平台官网:https://mp.weixin.qq.com/ 微信小程序官方开发文档:https://mp.weixin.qq.co ...
- jQuery获取URL的GET参数值
// jQuery url get parameters function [获取URL的GET参数值] // <code> // var GET = $.urlGet(); //获取UR ...
- mysql DATE_FORMAT 年月日时分秒格式化
SELECT DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s')
- LeetCode——Next Greater Element I
LeetCode--Next Greater Element I Question You are given two arrays (without duplicates) nums1 and nu ...
- Spring Boot 热部署的实现 - 原创
实现方式有两大种(其中包含3种): 一.基于springloaded 1.1)Maven启动方式 第一步:在pom.xml中的“plugin节点”里面添加如下依赖: <dependencies& ...
- LinuxKit
LinuxKit是Docker最新发布的一个用于为容器构建安全.便携.可移植操作系统的工具包.它根据用户编写的yaml(指定kernel和基于docker image的一些列服务)自动构建一个常见虚拟 ...
- angular2.x 多选框事件
angular2.x - 4.x 的多选框事件 ng2 -- ng4 反正都是用es6 都统称为2.x吧. 下面贴代码 html界面 <div class="row"> ...
- Selenium with Python 003 - 页面元素定位
WebUI自动化,首先需要定位页面中待操作的元素,然后进行各种事件操作,这里我们首先介绍Selenium Python 如何定位页面元素,WebDriver 提供了一系列的方法. 定位单个页面元素(返 ...