HDU 5489 Removed Interval(DP)

题意:求去掉某一个长度为L的子串的LIS

思路:画画图其实比较显然的想法是去掉这个区间的时候答案是右边以第一个数开头的LIS+左边最后一个数小于右边第一个数的LIS,为什么是右边以第一个数开头的LIS呢,因为如果是在这个L的后第二个是最佳答案的话那么我在这个“窗口”滑到L+1这个位置的时候左边会多一个位置,这样答案显然会一样或更优,所以每次只用考虑窗口的右边第一个数的LIS即可,将a[i]取负,然后从后往前做LIS,就是以第i个位置开始的LIS了,然后一边滑窗一边更新左边的LIS更新答案即可


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
#define inf 1e9
int a[maxn],g[maxn],dp[maxn],b[maxn];
int main()
{
    int T,cas=1;
	scanf("%d",&T);
	while(T--)
	{
		int n,L;
		scanf("%d%d",&n,&L);
		for(int i = 0;i<n;i++)
			scanf("%d",&a[i]),b[i]=-a[i];
		for(int i = 0;i<=n;i++)
			g[i]=inf;
		for(int i = n-1;i>=L;i--)
		{
			int x = lower_bound(g,g+n,b[i])-g;
            g[x]=b[i];
			dp[i]=x+1;
		}
        int ans = 0,maxlen = 0;
		for(int i = 0;i<=n;i++)
			g[i]=inf;
		for(int i = L;i<n;i++)
		{
			int x = lower_bound(g,g+n,a[i])-g;
			ans = max(ans,x+dp[i]);
			x = lower_bound(g,g+n,a[i-L])-g;
			g[x]=a[i-L];
			maxlen = max(maxlen,x+1);
		}
		ans = max(ans,maxlen);
		printf("Case #%d: %d\n",cas++,ans);
	}
}


Description

Given a sequence of numbers  , a subsequence   of   is referred as increasing if  . LY has just learned how to find the longest increasing subsequence (LIS). 
Now that he has to select   consecutive numbers and remove them from   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   indicating the number of test cases ( ). 
For each test case, the first line consists of two numbers   and   as described above ( ). The second line consists of   integers indicating the sequence. The absolute value of the numbers is no greater than 
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”.   is the test case number starting from 1.   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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值