zoukankan      html  css  js  c++  java
  • hdu 5773 The All-purpose Zero 线段树 dp

    The All-purpose Zero

    题目连接:

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

    Description

    ?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] <= 1000000).?? has a magic so that he can change 0 to any interger(He does not need to change all 0 to the same interger).?? wants you to help him to find out the length of the longest increasing (strictly) subsequence he can get.

    Input

    The first line contains an interger T,denoting the number of the test cases.(T <= 10)
    For each case,the first line contains an interger n,which is the length of the array s.
    The next line contains n intergers separated by a single space, denote each number in S.

    Output

    For each test case, output one line containing “Case #x: y”(without quotes), where x is the test case number(starting from 1) and y is the length of the longest increasing subsequence he can get.

    Sample Input

    2
    7
    2 0 2 1 2 0 5
    6
    1 2 3 3 0 0

    Sample Output

    Case #1: 5
    Case #2: 5

    Hint
    In the first case,you can change the second 0 to 3.So the longest increasing subsequence is 0 1 2 3 5.

    Hint

    题意

    给你n个数,其中0可以变成任何数,问你最长上升子序列可以是多少

    题解:

    考虑dp[i]表示长度为i的lis的最后一个数是多少。

    遇到0的时候,就相当于dp[i]原来等于v,现在dp[i+1]=v+1了,这样的转移。

    其实就相当于整体向右平移。

    这个我们就在线段树上预先留很多个位置,让起点向左边平移就好了嘛,嘿嘿嘿。

    代码

    #include <bits/stdc++.h>
    #define rep(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
    #define drep(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
    #define pb push_back
    #define mp make_pair
    #define sf scanf
    #define pf printf
    #define two(x) (1<<(x))
    #define clr(x,y) memset((x),(y),sizeof((x)))
    #define dbg(x) cout << #x << "=" << x << endl;
    const int mod = 1e9 + 7;
    int mul(int x,int y){return 1LL*x*y%mod;}
    int qpow(int x , int y){int res=1;while(y){if(y&1) res=mul(res,x) ; y>>=1 ; x=mul(x,x);} return res;}
    inline int read(){int 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;}
    using namespace std;
    const int maxn = 1e5 + 15;
    const int inf = 0x3f3f3f3f;
    int a[maxn],len[maxn],N,tot,C,Midpoint;
    
    struct Sgtree{
        struct node{
            int l , r , maxv , lazy;
    
            void Update( int x ){
                lazy += x;
                maxv += x;
            }
        }tree[maxn << 3];
    
        void ReleaseLabel( int o ){
            if( tree[o].lazy ){
                tree[o << 1].Update( tree[o].lazy );
                tree[o << 1 | 1].Update( tree[o].lazy );
                tree[o].lazy = 0 ;
            }
        }
    
        void Maintain( int o ){
            tree[o].maxv = max( tree[o << 1].maxv , tree[o << 1 | 1 ].maxv );
        }
    
        void Build(int l , int r , int o){
            tree[o].l = l , tree[o].r = r , tree[o].maxv = inf , tree[o].lazy = 0;
            if( r > l ){
                int mid = l + r >> 1;
                Build( l , mid , o << 1 );
                Build( mid + 1 , r , o << 1 | 1 );
                Maintain( o );
            }else if( l <= Midpoint ) tree[o].maxv = -5*N - 5;
        }
    
    
        void Modify( int ql , int qr , int v , int o ){
            int l = tree[o].l , r = tree[o].r;
            if( ql <= l && r <= qr ) tree[o].Update( v );
            else{
                int mid = l + r >> 1;
                ReleaseLabel( o );
                if( ql <= mid ) Modify( ql , qr , v , o << 1 );
                if( qr > mid ) Modify( ql , qr , v , o << 1 | 1 );
                Maintain( o );
            }
        }
    
        void Change( int x , int v , int o ){
            int l = tree[o].l , r = tree[o].r;
            if( l == r ) tree[o].maxv = min( tree[o].maxv , v );
            else{
                int mid = l + r >> 1;
                ReleaseLabel( o );
                if( x <= mid ) Change( x, v , o << 1 );
                else Change( x, v , o << 1 | 1 );
                Maintain( o );
            }
        }
    
        void Search( int v , int o ){
            int l = tree[o].l , r = tree[o].r;
            if( l == r ) tree[o].maxv = min( tree[o].maxv , v );
            else{
                int mid = l + r >> 1;
                ReleaseLabel( o );
                if( tree[o << 1].maxv >= v ) Search( v , o << 1 );
                else Search( v , o << 1 | 1 );
                Maintain( o );
            }
        }
    
        int Ask( int x , int o ){
            int l = tree[o].l , r = tree[o].r;
            if( l == r ) return tree[o].maxv;
            else{
                int mid = l + r >> 1;
                ReleaseLabel( o );
                int v;
                if( x <= mid ) v = Ask( x , o << 1 );
                else v = Ask( x , o << 1 | 1 );
                Maintain( o );
                return v;
            }
        }
    
    }Sgtree;
    
    
    void solve( int cas ){
        Midpoint = N + 5;
        Sgtree.Build( 1 , N * 2 + 500 , 1 );
        int extra = 0;
        rep(i,1,N){
            int x = a[i];
            if( x == 0 ){
                ++ Sgtree.tree[1].lazy; // 右移一位
                Sgtree.Change( -- Midpoint ,  -5*N - 5 , 1 );
                ++ extra;
                //cout << "i is " << i << endl;
                //rep(j,0,N+extra-1) pf("len[%d] is %d
    " , j , Sgtree.Ask(Midpoint+j,1));
                //cout << "---------" << endl;
            }else Sgtree.Search( x , 1 );
        }
        int mx = 0;
        rep(i,0,N+extra-1) if( Sgtree.Ask( Midpoint + i , 1 ) <= 10000000 )    mx = max( mx , i );
        pf("Case #%d: %d
    ",cas,mx);
    }
    
    int main(int argc,char *argv[]){
        int T=read(),cas=0;
        while(T--){
            N=read();
            rep(i,1,N) a[i] = read();
            solve( ++ cas );
        }
        return 0;
    }
  • 相关阅读:
    C#托盘图标
    线程相关整理
    Quartz.NET 快速入门
    (转)IE内存泄露,iframe内存泄露造成的原因和解决方案
    美化console.log的文本(转载)
    mongoDB学习资料整理
    EF7学习资料整理
    Oracle常用
    Node.js学习资料整理
    【大型网站技术实践】初级篇:借助Nginx搭建反向代理服务器(转)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5715906.html
Copyright © 2011-2022 走看看