hdu5489 ||2015合肥網路賽1006 dp+離散化樹狀陣列優化

life4711發表於2015-09-30

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

Removed Interval

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


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 (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 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
/**
hdu5489 ||2015合肥網路賽1006  dp+樹狀陣列優化
題目大意:給定一個序列,求去掉一個連續長度為L的數後的最長上升子序列的長度
解題思路:用dp[i][0]表示前i個數不去L時的最長子序列,dp[i][1]表示前i個數去L時的最長子序列。
          轉移方程為:dp[i][0]:它可以由0到i-1中滿足權值<=x_i的dp0_j轉移到.
                       dp[i][1]:它可以由0到i-1中滿足權值<=x_i的dp1_j以及0到i-L-1中滿足權值<=x_i的dp0_j轉移到
          由於資料範圍是10^5,需要離散化一下數字,用樹狀陣列維護。處理很巧妙,詳見程式碼
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=100100;
int a[maxn],num[maxn],n,m,L;
int dp[maxn][2],A[maxn],B[maxn];
bool cmp(int x,int y)
{
    return a[x]<a[y];
}
void add(int *A,int k1,int k2)
{
    for(; k1<=m; k1+=(k1&(-k1)))A[k1]=max(A[k1],k2);
}
int find(int *A,int k1)
{
    int ans=-1e9;
    for(; k1; k1-=(k1&(-k1)))ans=max(ans,A[k1]);
    return ans;
}
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&L);
        for(int i=1; i<=n; i++)scanf("%d",&a[i]);
        a[0]=-2e9;
        for(int i=0; i<=n; i++)num[i]=i;
        sort(num,num+n+1,cmp);
        m=1;
        int pre=a[num[0]];
        a[num[0]]=m;
        for(int i=1; i<=n; i++)
        {
            if(a[num[i]]!=pre)m++,pre=a[num[i]];
            a[num[i]]=m;
        }
//        for(int i=0;i<=n;i++) printf("%d ",a[i]);
//        printf("\n");
        for(int i=1; i<=m; i++)A[i]=0,B[i]=-1e9;
        if(L==0)for(int i=1; i<=m; i++)B[i]=0;
        for(int i=1; i<=n; i++)
        {
            dp[i][0]=find(A,a[i]-1)+1;
            dp[i][1]=find(B,a[i]-1)+1;
            add(A,a[i],dp[i][0]);
            add(B,a[i],dp[i][1]);
            if(i>=L) add(B,a[i-L],max(dp[i-L][0],dp[i-L][1]));
        }
        int ans=max(0,dp[n-L][0]);
        for(int i=1; i<=n; i++)ans=max(ans,dp[i][1]);
        printf("Case #%d: %d\n",++tt,ans);
    }
    return 0;
}


相關文章