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();
}