[luogu4309][最长上升子序列]

时间:2022-11-05 02:49:11

题目链接

思路

因为这些数字是从小到大加进去的,所以以当前数字结尾的最长上升子序列可以从前面位置的任何一个数字转移过来。所以只要能知道每个数字最终位于哪个位置就行了。

没想到出了treap还有什么办法求出来这个序列。看了眼题解发现用vector的insert直接模拟就能过。(纳尼?)这个函数不是应该是O(n)的吗。又手写了一个模拟,结果T了7个点。[luogu4309][最长上升子序列]

vector模拟

[luogu4309][最长上升子序列]

数组模拟

[luogu4309][最长上升子序列]

所以vector的复杂度是O(挺快)????

代码

/*
* @Author: wxyww
* @Date: 2018-12-02 14:49:17
* @Last Modified time: 2018-12-02 17:28:41
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<set>
#include<ctime>
#include<bitset>
#include<vector>
using namespace std;
typedef long long ll;
const int N = 100000 + 100;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int n;
int tree[N << 2];
namespace XDS {
void pushup(int rt) {
tree[rt] = max(tree[rt << 1],tree[rt << 1 | 1]);
}
void update(int rt,int l,int r,int pos,int c) {
if(l == r) {
tree[rt] = c;
return;
}
int mid = (l + r) >> 1;
if(pos <= mid) update(rt << 1,l,mid,pos,c);
else update(rt << 1 | 1,mid + 1,r,pos,c);
pushup(rt);
}
int query(int rt,int l,int r,int L,int R) {
if(L > R) return 0;
if(L <= l && R >= r) return tree[rt];
int mid = (l + r) >> 1;
int ans = 0;
if(L <= mid) ans = max(ans,query(rt << 1,l,mid,L,R));
if(R > mid) ans = max(ans,query(rt << 1 | 1,mid + 1,r,L,R));
return ans;
}
}
int a[N];
vector<int>b;
int main() {
n = read();
for (int i = 1; i <= n; i++)
b.insert(b.begin() + read(), i);
for(int i = 1;i <= n;++i) a[b[i - 1]] = i;
for(int i = 1;i <= n;++i) {
int k = XDS::query(1,1,n,1,a[i] - 1);
XDS::update(1,1,n,a[i],k + 1);
printf("%d\n",XDS::query(1,1,n,1,n));
}
return 0;
}

一言

我觉得,我这辈子最灿烂的笑容,大概都奉献给我电脑屏幕了。 ——bilibili