标签:
题意:动态插入数,求每次插完后的最长上升自序列长度。(N<=100000)
动态插入部分 块状链表
块状链表:
split:分裂 O(sqrt(n))
merge:合并 O(sqrt(n))
insert:插入 具体做法是找到对应的块O(sqrt(n)),在该块内找到对应的点O(sqrt(n))split,merge(first,now),merge(now,second)
maintain:整理 当两个相邻块sz和大于sqrt(n)时,merge,据说合并次数少,所以maintain操作也是O(sqrt(n))的。
dp部分参考上一篇题解吧……
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define N 400
#define inf 0x7fffffff
using namespace std;
struct node{
int a[N*2],sz;
node *next;
};
node *tmp1,*tmp2,*head;
int n,k,pos,num;
int a[N*N],d[N*N],ans[N*N];
void merge(node *t1,node *t2){
for(int i=t1->sz+1;i<=t1->sz+t2->sz;i++)
t1->a[i]=t2->a[i-t1->sz];
t1->sz+=t2->sz;
t1->next=t2->next;
delete t2;
}
void split(node *t1,int pos){
tmp2=new node;
for(int i=pos+1;i<=t1->sz;i++)
tmp2->a[i-pos]=t1->a[i],t1->a[i]=0;
tmp2->next=t1->next;t1->next=tmp2;
tmp2->sz=t1->sz-pos;t1->sz=pos;
}
void insert(int x,int pos){
if(head==NULL){
tmp1=new node;
head=tmp1;
tmp1->a[1]=x;
tmp1->next=NULL;
tmp1->sz=1;
return;
}
tmp1=head;
while(tmp1->sz<pos){
pos-=tmp1->sz;
tmp1=tmp1->next;
}
split(tmp1,pos);
tmp2=new node;
tmp2->a[1]=x;
tmp2->sz=1;
tmp2->next=tmp1->next;
tmp1->next=tmp2;
tmp1=head;
while(tmp1->next!=NULL){
if(tmp1->sz+tmp1->next->sz<=k) merge(tmp1,tmp1->next);
else tmp1=tmp1->next;
}
}
void getans(){
memset(d,127,sizeof(d));
d[0]=-inf;d[1]=a[1];ans[a[1]]=1;
int len=1;
for(int i=2;i<=n;i++){
int pos=upper_bound(d,d+len,a[i])-d;
//cout<<pos<<endl;
if(a[i]>d[len]) d[++len]=a[i],ans[a[i]]=len;
else d[pos]=min(d[pos],a[i]),ans[a[i]]=pos;
}
for(int i=1;i<=n;i++) ans[i]=max(ans[i],ans[i-1]);
for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
}
int main(){
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
scanf("%d",&n);k=(int)sqrt(n);
for(int i=1;i<=n;i++)
scanf("%d",&pos),insert(i,pos);
tmp1=head;
while(tmp1!=NULL){
for(int i=1;i<=tmp1->sz;i++) a[++num]=tmp1->a[i];
tmp1=tmp1->next;
}
getans();
return 0;
}
标签:
原文地址:http://blog.csdn.net/yxr0105/article/details/51364713