2019HDU多校第六场——HDU6635 Nonsense Time【树状数组求LIS】

阅读: 评论:0

2019HDU多校第六场——HDU6635 Nonsense Time【树状数组求LIS】

2019HDU多校第六场——HDU6635 Nonsense Time【树状数组求LIS】

题目链接: HDU6635 Nonsense Time

Time Limit: 14000/14000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

Problem Description

You a given a permutation p1,p2,…,pn of size n. Initially, all elements in p are frozen. There will be n stages that these elements will become available one by one. On stage i, the element pki will become available.

For each i, find the longest increasing subsequence among available elements after the first i stages.

Input

The first line of the input contains an integer T(1≤T≤3), denoting the number of test cases.

In each test case, there is one integer n(1≤n≤50000) in the first line, denoting the size of permutation.

In the second line, there are n distinct integers p1,p2,...,pn(1≤pi≤n), denoting the permutation.

In the third line, there are n distinct integers k1,k2,...,kn(1≤ki≤n), describing each stage.

It is guaranteed that p1,p2,...,pn and k1,k2,...,kn are generated randomly.

Output

For each test case, print a single line containing n integers, where the i-th integer denotes the length of the longest increasing subsequence among available elements after the first i stages.

Sample Input

1

5

2 5 3 1 4

1 4 5 3 2

Sample Output

1 1 2 3 3

题意:

给你一个1~n的排列a和排列b,起始状态a中所有数都是冻结的,i时刻a中b[i]这个数解冻,计算出当前a中已解冻的数的最长上升子序列。

分析:

正难则反,考虑倒着处理,按照b倒序冻结a中的数;

第n个时刻所有数都已解冻,树状数组求出当前的LIS并标记当前构成LIS,然后向前更新,如果当前冻结的数不在LIS中,那么不改变,否则再次求解LIS;

可以证明,这样的时间复杂度是。

Tips:类似链表的方式记录前后指针,给原序列加上前后端点,这样就可以快速的得到冻结某个数之后的序列。

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e4+7;
int a[maxn],b[maxn],vis[maxn],tag[maxn];
int LIS[maxn],bit[maxn],pre[maxn],nxt[maxn],ans[maxn];
int n;
int lowbit(int x) {return x&-x;}
void getLIS()
{for (int i=nxt[0];i<=n+1;i=nxt[i]){vis[i]=0;int tmp=0,res=a[i];while (res){if(LIS[tmp]<LIS[bit[res]]) tmp=bit[res];res-=lowbit(res);}LIS[i]=LIS[tmp]+1;tag[i]=tmp;res=a[i];while (res<=n+2){if(LIS[bit[res]]<LIS[i]) bit[res]=i;res+=lowbit(res);}}for (int i=nxt[0];i<=n+1;i=nxt[i]){int res=a[i];while (res<=n+2){bit[res]=0;res+=lowbit(res);}}for (int i=n+1;i;i=tag[i]) vis[i]=1;
}
void rua()
{scanf("%d",&n);for (int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]++;a[0]=1;a[n+1]=n+2;for (int i=0;i<=n+1;i++) {pre[i]=i-1;nxt[i]=i+1;vis[i]=bit[i]=0;}bit[n+2]=0;for (int i=1;i<=n;i++) scanf("%d",&b[i]);getLIS();for (int i=n;i;i--){ans[i]=LIS[n+1]-1;int x=b[i];pre[nxt[x]]=pre[x];nxt[pre[x]]=nxt[x];if(vis[x]) getLIS();}for (int i=1;i<n;i++) printf("%d ",ans[i]);printf("%dn",ans[n]);	
}
int main()
{int t;scanf("%d",&t);while (t--) rua();return 0;
}

 

本文发布于:2024-01-31 16:18:06,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170668908629803.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:树状   数组   第六场   HDU   LIS
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23