1. 程式人生 > >HDU 5489 Removed Interval

HDU 5489 Removed Interval

urn lan ems pac pre set blog print 它的

題目:http://acm.hdu.edu.cn/showproblem.php?pid=5489

題意:給定n個數,求從中刪除m個連續的數後,剩下的數中最大上升子序列的大小

我們可以枚舉刪除的區間

刪除後的答案為 以右邊第一個開始的最長上升子序列+以左邊第一個比它小的數結尾的最長上升子序列

前一項很好求,將數取反後反向跑一遍LIS

後一項可以考慮邊算邊求

在正向跑LIS的過程中,我們查詢右邊第一個數在左邊LIS中的位置

那麽它的前一個就是答案

最後不要忘記計算全在左邊的情況

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
const int N=1e5+5;
int a[N],b[N],dp[N],f[N];
int main()
{
    int T;
    scanf("%d",&T);
    for(int ca=1;ca<=T;ca++)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=-a[i];
        }
        memset(dp,0x3f,sizeof(dp));
        for(int i=n;i>=1;i--)
        {
            int t=lower_bound(dp+1,dp+1+n,b[i])-dp;
            f[i]=t;
            dp[t]=b[i];
        }
        memset(dp,0x3f,sizeof(dp));
        int ans=0;
        for(int i=m+1;i<=n;i++)
        {
            int t=lower_bound(dp+1,dp+1+n,a[i])-dp;
            ans=max(ans,t-1+f[i]);
            *lower_bound(dp+1,dp+1+n,a[i-m])=a[i-m];
        }
        int t=lower_bound(dp+1,dp+1+n,0x3f3f3f3f)-dp;
        ans=max(ans,t-1);
        printf("Case #%d: %d\n",ca,ans);
    }
    return 0;
}

  

HDU 5489 Removed Interval