1. 程式人生 > >hdu-5489/2015合肥網路賽 Removed Interval (LIS變形)

hdu-5489/2015合肥網路賽 Removed Interval (LIS變形)

                                     Removed Interval

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

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

一、原題地址

點我傳送

二、大致題意

  給出一串長度為 n 的串,求去掉長度為 L 的連續子串後剩餘的最長上升子序列。

三、思路

  對於每個點 i  我們先求出以該點為起點的上升子序列記為 g[ i ]。

  那麼接下來就是找到在範圍[ 0 , i-L-1 ] 內小於a[ i ]的數中,最長的上升子序列,那麼那個序列的長度pos加上g[ i ],就是當前應該更新的數值。然後依次更新就可以了。

四、程式碼

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long LL;


int n, m, num,L;
int T;
int a[100005], b[100005], tt[100005], g[100005];
int main()
{
	scanf("%d", &T);
	for (int K = 1; K <= T; K++)
	{
		scanf("%d %d", &n, &L);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			b[i] = -a[i];
		}
		memset(tt, inf, sizeof(tt));
		memset(g, 0, sizeof(g));
		for (int i = n; i >= 1; i--)
		{
			int pos = lower_bound(tt + 1, tt + 1 + n, b[i]) - tt;
			tt[pos] = b[i];
			g[i] = pos;
		}
		memset(tt, inf, sizeof(tt));
		a[n + 1] = inf;
		int ans = 0;
		for (int i = L + 1; i <= n + 1; i++)
		{
			int pos = lower_bound(tt, tt + n, a[i]) - tt;
			ans = max(ans, pos + g[i]);
			pos = lower_bound(tt, tt + n, a[i - L]) - tt;
			tt[pos] = a[i - L];
		}
		printf("Case #%d: %d\n",K, ans);
	}
	getchar();
	getchar();
}