HDU3564:Another LIS(线段树单点+LIS)

43 篇文章 0 订阅
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的插入顺序,要求每次插入之后的LIS

这道题还真是花费了不少时间,理解别人代码也花了很久时间,一直在纠结着如何去算LIS,前面还是很简单的,裸的线段树数空格问题,后面LIS的求法看别人的代码确实挺妙,这是用的将每个数的位置存起来,因为数值只有1~n,在这里确实很适合,只要记录了位置,再一次LIS即可。这个思想很巧妙,减一大家自己在演草纸上模拟下,大概思想就是,按照1~n循环下去,这个序列必定是递增的,再比较位置,因为数值递增,所以在保证LIS足够长的情况下,位置小的取小者,最后可以得到最长的值

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int n,s[1000000],dp[1000000],ans[1000000],len;
//ans[i]代表i在位置ans[i];
struct node
{
    int l,r,n;
} a[1000000];

void init(int l,int r,int i)
{
    a[i].l = l;
    a[i].r = r;
    if(l == r)
    {
        a[i].n = 1;
        return ;
    }
    int mid = (l+r)>>1;
    init(l,mid,i*2);
    init(mid+1,r,i*2+1);
    a[i].n = a[i*2].n+a[i*2+1].n;
}

void insert(int i,int x,int m)
{
    if(a[i].l == a[i].r)
    {
        ans[m] = a[i].l;
        a[i].n=0;
        return;
    }
    a[i].n--;
    if(x<=a[2*i].n)
        insert(2*i,x,m);
    else
        insert(2*i+1,x-a[2*i].n,m);
}

int bin(int k)
{
    int l = 1,r = len;
    while(l<=r)
    {
        int mid = (l+r)>>1;
        if(k>dp[mid])
            l = mid+1;
        else
            r = mid-1;
    }
    return l;
}

int main()
{
    int t,i,j,cas = 1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i = 1; i<=n; i++)
        {
            scanf("%d",&s[i]);
            dp[i] = 0;
        }
        init(1,n,1);
        printf("Case #%d:\n",cas++);
        for(i = n; i>0; i--)//典型的数空位线段树
            insert(1,s[i]+1,i);
        len = 0;
        for(i = 1; i<=n; i++)//LIS()
        {
            int k = bin(ans[i]);
            len = max(len,k);
            dp[k] = ans[i];
            printf("%d\n",len);
        }
        printf("\n");

    }

    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值