zoukankan      html  css  js  c++  java
  • hdu 5489——Removed Interval——————【删除一段区间后的LIS】

    Removed Interval

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


    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
     


    Source
     

    题目大意:给你n个元素的序列。让你移除长度为L的一段区间,让剩下的元素所形成的LIS最大。

    解题思路:枚举删除区间。同时更新删除区间后能形成的LIS最大值。并且每次更新删除区间的前面能形成的LIS。。。。。。。。。。。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e6+200;
    const int INF = 0x3f3f3f3f;
    int a[maxn],b[maxn],endminv[maxn];
    int dp[maxn];
    int BinSearch(int l,int r,int key,int typ){ //二分求解小于等于key的第一个位置
        int md;                                 //二分求解大于等于key的第一个位置
        if(typ==0){
            while(l<r){
                md=(l+r)/2;
                if(endminv[md]>key){
                    l=md+1;
                }else if(endminv[md]<key){
                    r=md;
                }else{
                    return md;
                }
            }
            return l;
        }else{
            while(l<r){
                md=(l+r)/2;
                if(endminv[md]>key){
                    r=md;
                }else if(endminv[md]<key){
                    l=md+1;
                }else{
                    return md;
                }
            }
            return l;
        }
    }
    
    int main(){
        int T,n,L,cnt=0;
        scanf("%d",&T);
        while(T--){
            scanf("%d%d",&n,&L);
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
            }
            for(int i=0;i<=n;i++)
                endminv[i]=-INF;
            for(int i=n;i>=1;i--){  //逆序得到最长递减子序列,那么正序依然是递增的
                int x=BinSearch(1,n,a[i],0);
                endminv[x]=a[i];
                dp[i]=x;    //以a[i]结尾的最长递减子序列长度
            }
            memset(endminv,INF,sizeof(endminv));
            int ans=0;  int len=1;
            for(int i=L+1;i<=n;i++){    //枚举要删除的区间的右端点
                int x=BinSearch(1,n,a[i],1);
                ans=max(ans,dp[i]+x-1);//删除区间后前后能拼接成的LIS长度
                x=BinSearch(1,n,a[i-L],1);  //枚举区间左边能形成的LIS
                endminv[x]=a[i-L];
                len=max(len,x+1);   //对于区间左边正常求LIS
            }
            printf("Case #%d: %d
    ",++cnt,max(ans,len-1));
        }
        return 0;
    }
    

      

  • 相关阅读:
    JSP页面中如何注入Spring容器中的bean
    struts2之Action与JSP相互数据传递
    IOS APP开发入门案例
    Android资源文件命名规范学习手册
    自定义控件之实现在线签名,保存,清除等功能实现
    SpringBoot+Mybatis+Maven+MySql小案例
    springBoot注解搜集
    SpringBoot打包war
    SpringBoot之返回json数据
    springboot之freemarker 和thymeleaf模板web开发
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4859613.html
Copyright © 2011-2022 走看看