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?
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.
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
Source
Recommend
wange2014
解题思路:对于【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;
}