Hdu 5489 Removed Interval【LIS+线段树】

Removed Interval

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1772    Accepted Submission(s): 583


Problem Description
Given a sequence of numbers  A=a1,a2,,aN , a subsequence  b1,b2,,bk  of  A  is referred as increasing if  b1<b2<<bk . LY has just learned how to find the longest increasing subsequence (LIS).
Now that he has to select  L  consecutive numbers and remove them from  A  for some mysterious reasons. He can choose arbitrary starting position of the selected interval so that the length of the LIS of the remaining numbers is maximized. Can you help him with this problem?
 

Input
The first line of input contains a number  T  indicating the number of test cases ( T100 ).
For each test case, the first line consists of two numbers  N  and  L  as described above ( 1N100000,0LN ). The second line consists of  N  integers indicating the sequence. The absolute value of the numbers is no greater than  109 .
The sum of N over all test cases will not exceed 500000.
 

Output
For each test case, output a single line consisting of “Case #X: Y”.  X  is the test case number starting from 1.  Y  is the maximum length of LIS after removing the interval.
 

Sample Input
  
  
2 5 2 1 2 3 4 5 5 3 5 4 3 2 1
 

Sample Output
  
  
Case #1: 3 Case #2: 1

题目大意:


给你一个长度为N的序列,让你删除一个连续长度为L的子序列,使得剩余序列的最长上升子序列的长度最大,问这个长度是多少。


思路:


①首先我们预处理出dp【i】表示以a【i】结尾的最长上升子序列长度,再预处理出dp2【i】表示以a【i】开头的最长上升子序列长度。


②然后我们接下来滑动窗口,假设有这样一种情况:

A B C [D E F] G H I J


其当前情况的解我们设定为:Dp2【G】+Max(Dp【A~C】&&要求选定的数要小于这个G才行)

这里我们Max(Dp【A~C】)我们显然可以离散化+线段树区间查询最大值能够得到。


那么接下来的过程中,我们只要滑动窗口维护最大值即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
int tree[100050*8];
int n,Len;
int a[100050];
int dp[100050];// 以a[i]结尾的LIS
int dp2[100050];// 以a[i]开头的LIS
int f[100050];
int b[100050];
void pushup(int rt)
{
    tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
int Query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return tree[rt];
    }
    else
    {
        int m=(l+r)>>1;
        int ans=0;
        if(L<=m)
        {
            ans=max(Query(L,R,lson),ans);
        }
        if(m<R)
        {
            ans=max(Query(L,R,rson),ans);
        }
        return ans;
    }
}
void build( int l ,int r , int rt )
{
    if( l == r )
    {
        tree[rt]=0;
        return ;
    }
    else
    {
        int m = (l+r)>>1 ;
        build(lson) ;
        build(rson) ;
        pushup(rt) ;
    }
}
void update(int p,int c,int l,int r,int rt)//p阵营c数据.
{
    if(l==r)
    {
        tree[rt]=max(tree[rt],c);
    }
    else
    {
        int m=(l+r)>>1;
        if(p<=m) update(p,c,lson);
        else  update(p,c,rson);
        pushup(rt);
    }
}
void Yu()
{
    memset(dp2,0,sizeof(dp2));
    memset(dp,0,sizeof(dp));
    memset(f,0x7f,sizeof(f));
    for(int i = 1; i <= n; ++i)
    {
        int k = lower_bound(f + 1, f+ 1 + n, a[i]) - f;
        dp[i] = k;
        f[k] = a[i];
    }
    memset(f,0x7f,sizeof(f));
    for(int i = n; i; --i)
    {
        int k = lower_bound(f + 1, f + 1 + n, -a[i]) - f;
        dp2[i] = k;
        f[k] = -a[i];
    }
}
int main()
{
    int t;
    int kase=0;
    scanf("%d",&t);
    while(t--)
    {
        map<int,int>s;
        scanf("%d%d",&n,&Len);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]),b[i]=a[i];
        int cnt=0;
        sort(b+1,b+1+n);
        for(int i=1;i<=n;i++)
        {
            if(s[b[i]]==0)
            s[b[i]]=++cnt;
        }
        Yu();
        int ans=0;
        build(0,n,1);
        for(int i=1;i<=n;i++)
        {
            int L=i,R=i+Len-1;
            if(R<=n)
            {
                if(i==1)ans=max(ans,dp2[R+1]);
                else if(R==n)ans=max(ans,Query(0,n,0,n,1));
                else
                {
                    ans=max(ans,dp2[R+1]+Query(0,s[a[R+1]]-1,0,n,1));
                }
                update(s[a[i]],dp[i],0,n,1);
            }
            else break;
        }
        printf("Case #%d: %d\n",++kase,ans);
    }
}









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值