Nonsense Time

阅读: 评论:0

Nonsense Time

Nonsense Time

Nonsense Time

时间限制: 10 Sec  内存限制: 128 MB
题目描述
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.
输入
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.
输出
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.
样例输入
1
5
2 5 3 1 4
1 4 5 3 2
样例输出
1 1 2 3 3

题意:有一个数列, 一开始这些数都不可用,接下来每次会让一个位置上的数变得可用,求每次操作后可用数的LIS。
思路:前置知识:长度为N的全排列的LIS的期望为sqrt(N),于是可以倒着让这些数变得不可用,如果它不是LIS上的数就对答案没影响,否则就暴力重新nlogn跑LIS。因为LIS的期望长度为sqrt(N),所以删除某一个数,该数是LIS上的数的概率是1/sqrt(N),也就是说期望会有sqrt(N)个数在LIS上,于是我们最多跑sqrt(N)遍暴力,期望复杂度:O(n*sqrt(n)*log(n))。
#include<bits/stdc++.h>
using namespace std;
const int N = 50050;
int arr[N],b[N]={0},len;
int k[N],vis[N]={0};
int pre[N];
int if_lis[N],id[N];int Serach(int num,int low,int high)
{int mid;while (low<=high) {mid=(low+high)>>1;if (num>=b[mid]) low=mid+1;else high=mid-1;}return low;
}void DP(int n)
{len=0;b[len]=-1;id[len]=-1;for(int i=1;i<=n;i++){if(!vis[i])continue;if(arr[i]>=b[len]){len++;b[len]=arr[i];id[len]=i;pre[i]=id[len-1];}else{int pos=Serach(arr[i],1,len);b[pos]=arr[i];pre[i]=id[pos-1];id[pos]=i;}}memset(if_lis,0,sizeof(if_lis));int now=id[len];while(now!=-1){if_lis[now]=1;now=pre[now];}
}int ans[N];
int main()
{int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&arr[i]);for(int i=1;i<=n;i++)scanf("%d",&k[i]);for(int i=1;i<=n;i++)vis[i]=1;DP(n);ans[n]=len;for(int i=n-1;i>=1;i--){vis[k[i+1]]=0;if(!if_lis[k[i+1]]){ans[i]=ans[i+1];continue;}DP(n);ans[i]=len;}for(int i=1;i<=n;i++)printf("%d%c",ans[i],i==n ? 'n' : ' ');}return 0;
}
View Code

 

转载于:.html

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

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

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

标签:Nonsense   Time
留言与评论(共有 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