hdu5489 Removed Interval(LIS变种+DP+二分)

题目链接
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 (T≤100).
For each test case, the first line consists of two numbers N and L as described above (1≤N≤100000,0≤L≤N). 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

Source
2015 ACM/ICPC Asia Regional Hefei Online

题意:给定初始序列a,现在要求删除连续的L个数,问删除后的最长LIS是多少?
思路:要删除连续L个数的话,我们肯定从【L+1,n】的每个数来考虑,假设我们删除1的L个数的结尾是i-1,那么对于i位置来说,它的LIS长度就为以i为开头的到n的LIS加上前面被删的LIS,这个讲的有点饶,我们可以先求出以i为开头的到n的LIS那就是代码里的dp【i】,然后求一下在i-L之前的LIS,对应代码里求得k,k+dp【i】-1就是答案。一个细节有处理一下,最后ans要和mxlen取max,因为不取max的话,把a【n】删掉得情况就会忽略。这里求LIS用了一个新方法。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+1;
const int inf=0x3f3f3f3f;
int T,n,l,a[maxn],b[maxn],x[maxn],dp[maxn];
int main()
{
    scanf("%d",&T);
    for(int j=1;j<=T;++j)
    {
        int ans=0,mxlen=0;
        scanf("%d%d",&n,&l);
        for(int i=1;i<=n;++i)
        scanf("%d",&a[i]),b[i]=-a[i];
        memset(x,inf,sizeof(x));
        for(int i=n;i>l;--i)
        {
            int k=lower_bound(x+1,x+1+n,b[i])-x;
            x[k]=b[i];
            dp[i]=k;
        }
        memset(x,inf,sizeof(x));
        for(int i=l+1;i<=n;++i)
        {
            int k=lower_bound(x+1,x+1+n,a[i])-x;
            ans=max(ans,k+dp[i]-1); 
            k=lower_bound(x+1,x+1+n,a[i-l])-x;
            x[k]=a[i-l];
            mxlen=max(mxlen,k);//以a【n】为结尾的l段删去的时候不要忘了特判 
        }
        ans=max(ans,mxlen);
        printf("Case #%d: %d\n",j,ans);
    }
 } 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值