HDU3564——Another LIS(线段树)

Another LIS
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 876    Accepted Submission(s): 307

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
 
Author
standy

Source
2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC
 

解析:

        由于当前每次插入会影响前面的位置,即前面数的位置由后面的操作决定。。。

        所以我们考虑倒推。。。建一颗线段树,kong记录当前节点对应区间剩余的位置个数。。。

        倒起将每次操作在线段树中维护出他的位置,再用nlogn的方法求lis

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100000+10;
int n,cha[N],pos[N],kong[4*N],c[N],len;

void read()
{
    freopen("lis.in","r",stdin);
    freopen("lis.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&cha[i]);
}

void up(int p)
{
    kong[p]=kong[p<<1]+kong[(p<<1)+1];
}

void build(int p,int l,int r)
{
    if(l==r)
    {
        kong[p]=1;
        return ;
    }
    int m=(l+r)>>1;
    build(p<<1,l,m);
    build((p<<1)+1,m+1,r);
    up(p);
}

void update(int p,int l,int r,int k,int t)
{
    if(l==r)
    {
        kong[p]--;pos[t]=l;
        return ;
    }
    kong[p]--;
    int m=(l+r)>>1;
    if(kong[p<<1]>=k)update(p<<1,l,m,k,t);
    else update((p<<1)+1,m+1,r,k-kong[p<<1],t);
    up(p);
}

int find(int x)
{
    int l=1,r=len,m;
    while(l<=r)
    {
        m=(l+r)>>1;
        if(x>c[m])l=m+1;
        else r=m-1;
    }
    return l;
}

void work()
{
    build(1,1,n);
    for(int i=n;i>=1;i--)
        update(1,1,n,cha[i]+1,i);//把i插到cha[i]+1位置
    for(int i=1;i<=n;i++)
    {
        int k=find(pos[i]);
        len=max(len,k);
        c[k]=pos[i];
        printf("%d\n",len);
    }
}

int main()
{
    read();
    work();
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值