HDU5489-Removed Interval(LIS变形)

216 篇文章 0 订阅
66 篇文章 0 订阅

Removed Interval

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


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
 

Recommend
wange2014
 

题意:有n个数,去掉连续的L个,问剩余的最长上升子序列长度是多少

解题思路:对于【L,n-1】里的数可以先处理出以a【i】为头的后面的LIS(可以从后往前处理)。对于某一位,可以假设出现在最终的序列中,以这一位为头的LIS已经处理好,然后还可以算出以这个数结尾,去掉前L个的LIS(类似于O(nlogn)计算LIS),看它在前面的LIS的哪里,两者相加-1就可以算出最终的LIS


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int a[100005], x[100005], b[100005], dp[100005];

int main()
{
	int t, cas=0;
	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];
		}
		memset(x, INF, sizeof x);
		for (int i = n - 1; i >= l; --i)
		{
			int k = lower_bound(x, x + n, b[i]) - x;
			x[k] = b[i];
			dp[i] = k + 1;
		}
		int ans = 0, mxlen = 0;
		memset(x, INF, sizeof x);
		for (int i = l; i < n; ++i)
		{
			int k = lower_bound(x, x + n, a[i]) - x;
			ans = max(ans, k + 1 + dp[i] - 1);
			k = lower_bound(x, x + n, a[i - l]) - x;
			x[k] = a[i - l];
			mxlen = max(mxlen, k + 1);
		}
		ans = max(ans, mxlen);
		printf("Case #%d: %d\n",++cas,ans);
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值