码迷,mamicode.com
首页 > 其他好文 > 详细

HDU5489 LIS变形

时间:2015-11-10 13:58:53      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

Removed Interval


Problem Description

Given a sequence of numbers A=a技术分享1技术分享,a技术分享2技术分享,,a技术分享N技术分享技术分享 , a subsequence b技术分享1技术分享,b技术分享2技术分享,,b技术分享k技术分享技术分享 of A技术分享 is referred as increasing if b技术分享1技术分享<b技术分享2技术分享<<b技术分享k技术分享技术分享 . 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 10技术分享9技术分享技术分享 .
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个元素的数组,删去k个连续数后,求LIS
题解:定义l[i]为  以第i个数结尾,删除i-k-1到i-1这k个数的LIS
        定义r[i]为 以i开头到n的LIS
       因为这样我们会忽略删除最后K个数的情况,所以我们n+1   最后一个元素赋值为无穷大
       答案就是 l[i]+r[i]-2;
技术分享
///1085422276

#include<bits/stdc++.h>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){
        if(ch==-)f=-1;ch=getchar();
    }
    while(ch>=0&&ch<=9){
        x=x*10+ch-0;ch=getchar();
    }return x*f;
}
//****************************************
const int  N=100000+50;
#define mod 1000000007
#define inf 1000000007

int b[N],a[N],dp[N],l[N],r[N],n,L;

int main() {
  int T=read();int oo=1;
  while(T--) {
     mem(a),mem(b);
     scanf("%d%d",&n,&L);
     for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
     }a[++n]=inf;
     for(int i=1;i<=n;i++) {
        b[n-i+1]=-a[i];
     }
     fill(dp+1,dp+n+1,inf+2);
     for(int i=L+1;i<=n;i++) {
        int tmp=lower_bound(dp+1,dp+n+1,a[i])-dp;
        l[i]=tmp;
        tmp=lower_bound(dp+1,dp+n+1,a[i-L])-dp;
        dp[tmp]=a[i-L];
     }
     fill(dp+1,dp+n+1,inf);
     for(int i=1;i<=n;i++) {
        int tmp=lower_bound(dp+1,dp+n+1,b[i])-dp;
        r[n-i+1]=tmp;
        dp[tmp]=b[i];
     }
     //for(int i=1;i<=n;i++)cout<< l[i]<<" ";
     //cout<<endl;
     //for(int i=1;i<=n;i++)cout<< r[i]<<" ";
     int ans=0;
     for(int i=L+1;i<=n;i++) {
        ans=max(ans,l[i]+r[i]-2);
     }
     printf("Case #%d: ",oo++);
     cout<<ans<<endl;
  }
  return 0;
}
代码

 

HDU5489 LIS变形

标签:

原文地址:http://www.cnblogs.com/zxhl/p/4952511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!