HDU 5489 二分 LIS

Removed Interval

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

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
 
Source
题意:给你长度为n的序列  现在删除长度为L的连续的一段   问如何删除使得剩下的部分的LIS最大 输出最大值
题解:先处理一遍LIS dp[i]  表示以a[i]结尾的最长上升子序列的长度  对于每一段的长度L  ans=dp[i]+{i+L之后的以大于a[i]的值为起点的最长上升的长度}
与网上题解不同 这里是倒着来的。
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
#include<set>
#define ll __int64
#define mod 100000000
#define N 5e6+10
#define M 1e
using namespace std;
int t;
int n,l;
int a[],b[];
int dp[];
int ans[];
int main()
{
scanf("%d",&t);
for(int i=;i<=t;i++)
{
scanf("%d %d",&n,&l);
a[]=;
for(int j=;j<=n;j++){
scanf("%d",&a[j]);
b[j]=-a[j];
}
int exm=;
ans[exm]=a[];
dp[]=;
for(int j=;j<=n;j++)
{
if(a[j]>ans[exm]){
exm++;
ans[exm]=a[j];
dp[j]=exm;
}
else
{
int pos=lower_bound(ans+,ans+exm,a[j])-ans;
ans[pos]=a[j];
dp[j]=pos;
}
}
int an=;
for(int j=;j<=n;j++)
ans[j]=1e9;
int re=;
for(int j=n-l;j>=;j--)
{
int x=lower_bound(ans,ans+n,b[j])-ans;//
an=max(an,dp[j]+x);
int y=lower_bound(ans,ans+n,b[j+l])-ans;
ans[y]=b[j+l];
re=max(re,y+);
}
printf("Case #%d: %d\n",i,max(an,re));
}
return ;
}
/*
6
6 2
1 3 5 7 2 4
*/
上一篇:IOS 制作启动画面


下一篇:windows service宿主web api使用"依赖注入"和“控制反转”的技术实践