zoukankan      html  css  js  c++  java
  • hdu5489 Removed Interval

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


    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

    这题想了很长时间,题意是求切去长度为l的连续子序列后,剩下的序列的最长上升子序列,切的起始位置随意定。

    可以记录两个函数

    f[i]:以a[i]为尾端的lis的最大长度。

    g[i]:以a[i]为起始点的lis的最大长度。两者都包含a[i]。

    这样对于每一个点,我们可以根据i, 找到 [0,i−L−1]之间的一个值,使得这个值小于a[i],且长度最大,可以用线段树来维护。然后用maxx=max(maxx,ans+g[i]),即前半段加上后半段就行了。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define maxn 100060
    int f[maxn],g[maxn],pos[maxn],a[maxn],c[maxn],a1[maxn],h[maxn];
    struct node{
        int l,r,maxnum;
    }b[4*maxn];
    
    void build(int l,int r,int i)
    {
        int mid;
        b[i].l=l;b[i].r=r;b[i].maxnum=0;
        if(l==r)return;
        mid=(l+r)/2;
        build(l,mid,i*2);
        build(mid+1,r,i*2+1);
    }
    
    void update(int idx,int num,int i)
    {
        int mid;
        if(b[i].l==idx && b[i].r==idx){
            b[i].maxnum=max(b[i].maxnum,num);return;
        }
        mid=(b[i].l+b[i].r)/2;
        if(idx<=mid)update(idx,num,i*2);
        else update(idx,num,i*2+1);
        b[i].maxnum=max(b[i*2].maxnum,b[i*2+1].maxnum);
    }
    
    int question(int l,int r,int i)
    {
        int mid;
        if(l>r)return 0;
        if(b[i].l==l && b[i].r==r){
            return b[i].maxnum;
        }
        mid=(b[i].l+b[i].r)/2;
        if(r<=mid) return question(l,r,i*2);
        else if(l>mid)return question(l,r,i*2+1);
        else{
            int lv=question(l,mid,i*2);
            int rv=question(mid+1,r,i*2+1);
            return max(lv,rv);
        }
    
    }
    
    int main()
    {
        int n,m,i,j,T,l,tot,len,maxx,ans,num1=0,k;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&l);
            tot=0;
            for(i=1;i<=n;i++){
                scanf("%d",&a[i]);
                pos[i]=a[i];
            }
            sort(pos+1,pos+1+n);
            tot=unique(pos+1,pos+1+n)-pos-1;
            for(i=1;i<=n;i++)a[i]=lower_bound(pos+1,pos+1+tot,a[i])-pos;
            f[1]=1;len=1;c[1]=a[1];
            for(i=2;i<=n;i++){
                if(a[i]>c[len]){
                    len++;f[i]=len;
                    c[len]=a[i];continue;
                }
                j=lower_bound(c+1,c+1+len,a[i])-c;
                c[j]=a[i];
                f[i]=j; //这里很重要,是j,不是len,因为求的是以a[i]为结尾的最长长度,不是前i个的最长长度
            }
    
            for(i=1;i<=n;i++){
                a1[i]=-a[n+1-i];
            }
    
            g[1]=1;len=1;c[1]=a1[1];
            for(i=2;i<=n;i++){
                if(a1[i]>c[len]){
                    len++;g[i]=len;
                    c[len]=a1[i];continue;
                }
                j=lower_bound(c+1,c+1+len,a1[i])-c;
                c[j]=a1[i];
                g[i]=j;
            }
            reverse(g+1,g+1+n);
    
            build(1,100000,1);
            maxx=0;
            for(i=l+1;i<=n;i++){
                ans=question(1,a[i]-1,1);
                update(a[i-l],f[i-l],1);
                maxx=max(maxx,ans+g[i]);
            }
            if(n-l>=1){
                maxx=max(maxx,f[n-l]);
            }
            num1++;
            printf("Case #%d: %d
    ",num1,maxx);
        }
        return 0;
    }
    /*
    2
    10 2
    1 2 3 9 8 7 4 5 6 7
    */
    

     
  • 相关阅读:
    使用rpmbuild打包erlang和rabbitmq进行部署服务的方法
    Linux 通过yum 方式离线下载依赖rpm包的操作步骤
    影响产品运行性能的参数统计- 不能相信浏览器编辑器 一次崩溃数据全丢 垃圾的要死
    SpringMVC异步处理(Callable和DeferredResult)
    windows 服务自动启动脚本 监控windows服务,并且自动启动
    mysql中局部变量_MySQL中变量的总结
    CompletableFuture实现异步获取结果并且等待所有异步任务完成
    git合并分支上的多条commit为一条commit到master,合并多条commint.合并多条提交
    mysqldumpslow不是内部或外部命令,也不是可运行的程序,mysql慢查询日志工具
    @Validated和@Valid区别:Spring validation验证框架对入参实体进行嵌套验证必须在相应属性(字段)加上@Valid而不是@Validated
  • 原文地址:https://www.cnblogs.com/herumw/p/9464647.html
Copyright © 2011-2022 走看看