Nonsense Time

题目描述

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的nlogn算法,运用二分

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod=1e9+7;
const int modp=998244353;
const int maxn=5e4+50;
const double eps=1e-6;
#define lowbit(x)  x&(-x)
#define INF 0x3f3f3f3f
const double inf=0x7fffffff;
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
int dcmp(double x)
{
    if(fabs(x)<eps)return 0;
    return (x>0)?1:-1;
}
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
ll qmod(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=(ans*a)%mod;
        }
        b>>=1;
        a=(a*a)%mod;
    }
    return ans;
}
int n;
int a[maxn],dp[maxn],pos[maxn],vis[maxn],ans[maxn],len[maxn];
void lis(){
    for(register int i=0;i<=n;++i){
        dp[i]=INF;
        len[i]=-1;
        vis[i]=0;
    }
    int maxx=0;
 
    for(register int i=0;i<n;++i)
    {
        if(a[i]!=-1){
            int pos=lower_bound(dp,dp+n,a[i])-dp;
            if(dp[pos]==INF){
                len[i]=pos;
                dp[pos]=a[i];
                maxx=max(pos,maxx);
            }
            else{
                len[i]=pos;
                dp[pos]=a[i];
            }
        }
    }
    for(register int i=n-1;i>=0;--i){
        if(maxx<0){
            break;
        }
        if(len[i]==maxx){
            vis[i]=1;
            maxx--;
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        n=read();
        for(register int i=0;i<n;++i){
            a[i]=read();
        }
        lis();
        for(register int i=0;i<n;++i){
            pos[i]=read();
            --pos[i];
        }
 
        for(register int i=n-1;i>=0;--i){
            ans[i]=lower_bound(dp,dp+n,INF)-dp;
            if(vis[pos[i]]==1){
                a[pos[i]]=-1;
                lis();
            }
            else{
                a[pos[i]]=-1;
            }
        }
 
        for(register int i=0;i<n-1;i++){
            printf("%d ",ans[i]);
        }
        printf("%d\n",ans[n-1]);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值