hdu 5489 Removed Interval 动态规划,最长上升子序列

Removed Interval

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


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
 

Source
 

假设最长子序列在被删除位置的起点的左边,即满足 i < n - L

那么答案就是【0,i】中i可以排到的最长的位置

假设删除区间在最长子序列中间部分

枚举每一个点x,删除位置为[x-l-1,x-1]。

那么计算出[0,x-l-1]的最长上升序列的情况,然后看第x个数能加入这些序列最长的位置y。那么答案就是

y + x能够出现在[x,n]区间中以x开头的最长子序列的长度(相当于逆序求x所在最长下降子序列的的长度)。

证明如下:

     对于一个x,删除[x-l-1,x-1]的位置得到的结果最优。

    显然对于x来说得到的结果是[0,x-l-1]+[x,n]包含x的最长子序列长度。

    那么可能的情况是 存在y,y的位置在[x-l-1,x-1]区间,且x,y同时存在这个最长的子序列结果中。

    那么显然在[y,n]区间x会被计算到关于y必选的子序列的情况中。并且关于y的最长的子序列的长度已经在之前计算得到。



#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define maxn 100007
int dp[maxn];
int rs[maxn],ls[maxn];
int num[maxn];

int find(int n,int val){
    int low = 0,high = n-1;
    while(low <= high){
        int mid = (low+high)/2;
        if(dp[mid] >= val) high = mid-1;
        else low = mid+1;
    }
    return low;
}

int main(){
    int t,n,l,tt=1;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&l);
        for(int i = 0;i < n; i++)
            scanf("%d",&num[i]);
        int cnt = 0;
        for(int i = n-1;i >= 0; i--){
            int p = find(cnt,-num[i]);
            dp[p] = -num[i];
            rs[i] = p;
            if(p == cnt) cnt++;
        }
        cnt = 0;
        int ans = 0;
        memset(ls,0,sizeof(ls));
        for(int i = 0;i < n; i++){
            if(i+l < n){
                int p = find(cnt,num[i+l]);
                ls[i+l] = p;
            }
            int p = find(cnt,num[i]);
            dp[p] = num[i];
            if(p == cnt) cnt++;
            if(i < n - l) ans = max(ans,p+1);
        }

        for(int i = l;i < n; i++){
            ans = max(ans,rs[i]+ls[i]+1);
        }
        printf("Case #%d: %d\n",tt++,ans);
    }
    return 0;
}







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值