HDU 3564 Another LIS 线段树+最长递增子序列

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个数字,求每一次插入后的最长递增子序列。

这道题对与初学线段树的简直可以说是噩梦,无奈只好搜题解。

发现竟是如此的巧妙。

建立一个线段树,表示区间内的空位数。

然后倒着遍历数组所需要插入的位置,倒着插入的好处是不用挪动其他元素了,因为后面的元素的位置已经确定好了。 线段树是区间求和。 

然后通过一个数组no记录下1-N数字的位置。

接下来是对no求最长连续递增子序列。

这里用的是二分查找法, 时间复杂度为N*lonN。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=100005;
const int INF=0x3f3f3f3f;
int t;
int tree[maxn<<2];
int a[maxn];
int no[maxn];
int dp[maxn];
int n;
void pushup (int re)
{
    tree[re]=tree[re<<1]+tree[re<<1|1];
}
void build (int l,int r,int re)
{
    if(l==r)
    {
       tree[re]=1;
       return ;
    }
    int mid=(l+r)>>1;
    build (l,mid,re<<1);
    build (mid+1,r,re<<1|1);
    pushup (re);
}
//找到插入位置并更新线段树
void update (int data,int l,int r,int re,int zz)
{
    if(l==r)
    {
       tree[re]--;
       no[zz]=l;
       return;
    }
    int mid=(l+r)>>1;
    if(data<=tree[re<<1])
        update (data,l,mid,re<<1,zz);
    else
        update (data-tree[re<<1],mid+1,r,re<<1|1,zz);
    pushup (re);
}
//二分查找
int finds (int a,int len)
{
    int l=1,r=len;
    while (l<=r)
    {
        int mid=(l+r)>>1;
        if(dp[mid]<=a)
            l=mid+1;
        else if(dp[mid]==a)
            return mid;
        else
            r=mid-1;
    }
    return l;
}
int main()
{
    scanf("%d",&t);
    for (int i=1;i<=t;i++)
    {
        scanf("%d",&n);
        build (1,n,1);
        printf("Case #%d:\n",i);
        for (int j=1;j<=n;j++)
        {
           scanf("%d",&a[j]);
        }
        for (int j=n;j>=1;j--)
        {
           update (a[j]+1,1,n,1,j);
        }
        //求最长递增子序列
        int len=1;
        dp[1]=no[1];
        printf("1\n");
        for (int j=2;j<=n;j++)
        {
            if(dp[len]<no[j])
            {
                dp[++len]=no[j];
            }
            else
            {
                int pos=finds(no[j],len);
                dp[pos]=no[j];
            }
            printf("%d\n",len);
        }
        printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值