HDU - 5489 Removed Interval(最长上升子序列)

13 篇文章 0 订阅
4 篇文章 0 订阅

HDU - 5489 Removed Interval(最长上升子序列)

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

题目大意:

给定n,m
问:从长度为n的序列中删除连续的m个后的最长上升子序列的长度。

解题思路:

提前用nlogn的复杂度,处理出以每个点为开始的最长上升子序列的长度。
然后枚举我们要删除的区间 像个长度为m的窗口一样从左往右滑动,更新ans。

  • 1.怎么样用n logn的复杂度处理出每个点的最长上升子序列?
    我们用b数组,存a的相反数,为什么要存相反数呢?下面会说到。
    arr数组维护一个上升的序列 ,也就是我们的最长上升子序列。
    d数组,就是存答案 ,d[i]表示以第i个开始的最长上升子序列的长度。

代码如下:

 for(int i=n-1;i>=0;i--){
           int id = lower_bound(arr,arr+n,b[i]);
           arr[id] = b[i];
           d[i] = id+1;
  }

我们假设求序列
3 5 2 6 7 1 8 的最长上升子序列
那么
a = [ 3 5 2 6 7 1 8]
b = [-3 -5 -2 -6 -7 -1 -8]
我们从右往左往arr里面插
先从arr中找>= -8的第一个数的位置
在这里插入图片描述
把-8插到0的位置,也就说-8后面没有比它大的了,d[i] = id+1 即d[6] = 1
因为下标是从0开始所以要 +1
在这里插入图片描述
然后下一个找arr 中大于等于-1的第一个,这时候我们就应该懂了为什么要全变相反数了,因为是从右往左插入arr中的 ,对于一个数x在arr中排在第i位上,就说明 它的前面有 i个比他小的 也就是对于a数组来说 右边有多少比他大的。
插入-1后 发现id是1 1这个数所能形成的最长上升子序列长度为 id+1 = 2;
在这里插入图片描述
再插 -7 发现id = 1 所以 7这个位置能形成的最长上升子序列为id+1 = 2在这里插入图片描述
依次往下插b数组,就能得到d数组。

  • 处理出最长上升子序列后怎么解决这个问题呢?
    我们枚举它所跳过的m个
		memset(arr,inf,sizeof(arr));
        for(int i=m;i<n;i++){
            int id = lower_bound(arr,arr+n,a[i])-arr;
            ans=max(ans,id+d[i]);
            id = lower_bound(arr,arr+n,a[i-m])-arr;
            arr[id] = a[i-m];
            left_len = max(left_len,id+1);
        }
        ans = max(ans,left_len);

ans 就是答案,left_len就是左边所能形成的最长上升子序列。
所用的思想跟 处理d数组的思想类似,类似于树状数组求逆序数的思想。

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
const int inf = 0x3f3f3f3f;
int a[maxn];
int b[maxn];
int arr[maxn];
int d[maxn];
int n,m;
int main(){
    int t;
    cin>>t;
    int cas = 1;
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
            b[i] = -a[i];
        }
        memset(arr,inf,sizeof(arr));
        for(int i=n-1;i>=0;i--){
            int id = lower_bound(arr,arr+n,b[i])-arr;
            arr[id] = b[i];
            d[i] = id+1;
        }
        int ans = 0;
        int left_len = 0;
        memset(arr,inf,sizeof(arr));
        for(int i=m;i<n;i++){
            int id = lower_bound(arr,arr+n,a[i])-arr;
            ans=max(ans,id+d[i]);
            id = lower_bound(arr,arr+n,a[i-m])-arr;
            arr[id] = a[i-m];
            left_len = max(left_len,id+1);
        }
        ans = max(ans,left_len);
        printf("Case #%d: %d\n",cas++,ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值