hdu 3564 Another LIS(线段树+lis)

Problem Description
There is a sequence firstly empty. We begin to add number from 1 to N to the sequence, and every time we just add a single number to the sequence at a specific position. Now, we want to know length of the LIS (Longest Increasing Subsequence) after every time's add.
 

Input
An integer T (T <= 10), indicating there are T test cases.
For every test case, an integer N (1 <= N <= 100000) comes first, then there are N numbers, the k-th number Xk means that we add number k at position Xk (0 <= Xk <= k-1).See hint for more details.
 

Output
For the k-th test case, first output "Case #k:" in a separate line, then followed N lines indicating the answer. Output a blank line after every test case.
 

Sample Input
   
   
1 3 0 0 2
 

Sample Output
   
   
Case #1: 1 1 2
Hint
In the sample, we add three numbers to the sequence, and form three sequences. a. 1 b. 2 1 c. 2 1 3
 

题意:依次给出1~n的插入顺序pos,如果该位置有人,则已有的后移一位,要求每次插入之后的LIS;
思路:求lis,我们需要先求出每个数的所在位置,对于给出的插入顺序,对于最后面的位置是固定的,我们可以利用线段树先从最后一个依次插入,用ans数组记录每个数的位置;
最后对于给出的每个数的位置用二分的lis得出结果;
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100005;
int a[N],ans[N],dp[N],len;
struct node{
    int l,r,w;
}str[3*N];
void build(int l,int r,int n)
{
    str[n].l=l;
    str[n].r=r;
    if(l==r)
    {
        str[n].w=1;
        return;
    }
    int temp=(l+r)/2;
    build(l,temp,2*n);
    build(temp+1,r,2*n+1);
    str[n].w=str[2*n].w+str[2*n+1].w;
}
void inser(int n,int w,int pos)
{
    if(str[n].l==str[n].r)
    {
        str[n].w=0;
        ans[pos]=str[n].l;
        return;
    }
    str[n].w--;
    int temp=(str[n].l+str[n].r);
    if(w<=str[2*n].w)
         inser(2*n,w,pos);
    else
         inser(2*n+1,w-str[2*n].w,pos);
}
int find(int x)
{
    int l=1,r=len;;
    while(l<r)
    {
        int temp=(l+r)/2;
        if(ans[x]<=dp[temp])
            r=temp;
        else
            l=temp+1;
    }
    return l;
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;t++)
    {
        int n;
        scanf("%d",&n);
        build(1,n,1);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=n;i>=1;i--)
        {
            inser(1,a[i]+1,i);
        }
        printf("Case #%d:\n",t);
        len=1;
        dp[1]=ans[1];
        printf("1\n");
        int temp;
        for(int i=2;i<=n;i++)
        {
            if(dp[1]>ans[i]) temp=1;
            else if(dp[len]<ans[i]) len++,temp=len;
            else temp=find(i);
            dp[temp]=ans[i];
            printf("%d\n",len);
        }
        printf("\n");
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值