• 常用算法模板


    开始存模板

    快速排序

    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    
    using namespace std ; 
    typedef long long ll ; 
    const int maxN = 100010 ; 
    
    int a[ maxN ] ; 
    
    inline ll INPUT ( ) {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(x=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
        return x*f;
    } 
    
    void quick_sort ( const int l , const int r ) {
        if ( l > r ) {
            return ; ;
        }
        int i = l , j = r ; 
        while ( i < j ) {
            while ( a[ j ] >= a[ l ] && j > i ) {
                --j ; 
            }
            while ( a[ i ] <= a[ l ] && j > i ) {
                ++i ; 
            }
            if ( j > i )
                swap ( a[ i ] , a[ j ] ) ; 
        }
        swap ( a[ l ] , a[ i ] ) ;
        quick_sort ( l , i - 1 ) ;
        quick_sort ( i + 1 , r ) ; 
    }
    
    int main ( ) {
        int N = INPUT( ) ;
        for ( int i=1 ; i<=N ; ++i ) {
            a[ i ] = INPUT( ) ; 
        }
        quick_sort ( 1 , N ) ; 
        for ( int i=1 ; i<=N ; ++i ) {
            cout << a[ i ] << endl ; 
        }
        return 0 ; 
    }
    View Code

    快速选择

    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    
    using namespace std ; 
    typedef long long ll ; 
    const int maxN = 100010 ; 
    
    int a[ maxN ] ; 
    
    inline ll INPUT ( ) {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(x=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
        return x*f;
    } 
    
    int quick_select ( const int l , const int r , const int K ) {
        int i = l , j = r ; 
        while ( i < j ) {
            while ( a[ j ] >= a[ l ] && j > i ) {
                --j ; 
            }
            while ( a[ i ] <= a[ l ] && j > i ) {
                ++i ; 
            }
            if ( j > i )
                swap ( a[ i ] , a[ j ] ) ; 
        }
        swap ( a[ l ] , a[ i ] ) ;
        
        if ( i - l == K - 1 ) 
           return a[ i ] ;
           
        else if ( i - l >= K ) 
           return quick_select ( l , i - 1 , K ) ;
        
        else
           return quick_select( i + 1 , r , K - ( i - l + 1 ) ) ;
    }
    
    int main ( ) {
        int N = INPUT ( ) ;
        int K = INPUT ( ) ; 
        for ( int i=1 ; i<=N ; ++i ) {
            a[ i ] = INPUT( ) ; 
        }
        cout << quick_select ( 1 , N , K ) << endl ;
        return 0 ; 
    }
    View Code

    树的重心

    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std ; 
    const int maxN = 20010 ; 
    const int inf = 2147483647 ; 
    
    struct Edge {
        int to , next ; 
    }e[ maxN << 1 ] ;
    
    int head[ maxN ] , size[ maxN ] , hv[ maxN ] ; 
    int _cnt ; 
    
    int INPUT ( ) {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
        return x*f;
    }
    
    void addEdge ( int x , int y ) {
        e[ ++_cnt ].to = y ; 
        e[ _cnt ].next = head[ x ] ; 
        head[ x ] = _cnt ; 
    }
    
    void Dfs ( const int x , int fa ) {
        size[ x ] = 1 ; 
        for ( int i=head[ x ] ; i ; i=e[ i ].next ) {
            if ( e[ i ].to != fa ) {
                Dfs ( e[ i ].to , x ) ; 
                size[ x ] += size[ e[ i ].to ] ; 
            }
        }
        for ( int i=head[ x ] ; i ; i=e[ i ].next ) {
            if ( e[ i ].to != fa && size[ e[ i ].to ] > size[ hv[ x ] ] ) {
                hv[ x ] = e[ i ].to ;
            }
        }
    }
    void init ( ){
        _cnt = 0 ; 
        memset ( size , 0 , sizeof ( size ) ) ;
        memset ( hv , 0 , sizeof ( hv ) ) ;
        memset ( head , 0 , sizeof ( head ) ) ;
    }
    
    int main ( ) {
        for ( int T = INPUT ( ) ; T ; --T ) {
            init ( ) ; 
            int N = INPUT ( ) ;
            for ( int i=1 ; i<N ; ++i ) {
                int x = INPUT ( ) , y = INPUT ( ); 
                addEdge ( x , y ) ;
                addEdge ( y , x ) ; 
            }
            Dfs ( 1 , 1 ) ; 
            int tmp , minNode = 0 , minval = inf ; 
            for ( int i=1 ; i<=N ; ++i ) {
                int tmp = max ( size[ hv[ i ] ] , N - size[ i ] ) ;
                if ( tmp < minval ) {
                    minval = tmp ; 
                    minNode = i ; 
                }
            }
            printf ( "%d %d
    " , minNode , minval ) ; 
        }
        return 0 ;  
    }
    poj1655

    Tire树

    #include <bits/stdc++.h>
    
    using namespace std ;
    const int maxN = 5010 ;
    typedef long long LL ; 
    
    struct tireNode {
        int cnt ; 
        bool last ; 
        struct tireNode *next[ 26 ] ; 
        tireNode ( ) {//构造方法初始化 
            cnt = 0 ; 
            last = false ; 
            memset ( next , 0 , sizeof ( next ) ) ;
        }
    }; 
    
    char str_[ maxN ] ;
    
    void insert ( tireNode *root , char str[ ] ) {
        int len = strlen ( str + 1 ) ; 
        tireNode *cur = root ;
        
        for ( int i=1 ; i<=len ; ++i ) {
            char ch = str[ i ] - 'a' ; 
            if ( cur -> next[ ch ] == NULL ) {
                tireNode* newNode = new tireNode ; 
                cur -> next[ ch ] = newNode ; 
            } 
            cur = cur -> next[ ch ] ;
            cur->cnt++ ; 
        }
        cur -> last = true ;
    }
    
    bool find ( tireNode *root , char str[ ] ) {
        int len = strlen ( str + 1 ) ; 
        tireNode *cur = root ; 
        for ( int i=1 ; i<=len ; ++i ) {
            char ch = str[ i ] - 'a' ; 
            if ( cur -> next[ ch ] == NULL ) return false ;
            cur = cur -> next[ ch ] ; 
        }
        if ( cur -> last == true ) return true ; 
        return false ; 
    }
    
    int main ( ) {
        int N , M ; 
        tireNode* root = new tireNode ; 
        scanf ( "%d" , &N ) ; 
        for ( int i=1 ; i<=N ; ++i ) {
            scanf ( "%s" , str_ + 1 ) ; 
            insert ( root , str_ ) ; 
        }
        scanf ( "%d" , &M ) ; 
        for ( int i=1 ; i<=M ; ++i ) {
            scanf ( "%s" , str_ + 1 ) ; 
            if ( find ( root , str_ ) ) printf ( "Yes
    " ) ;
            else printf ( "No
    " ) ;  
        }
        return 0 ; 
    }
    动态字典树
    #include <bits/stdc++.h>
    
    using namespace std ;
    const int maxNode = 10010 ; 
    typedef long long LL ; 
    
    struct tireTree {
        static const int maxLen = 26 ; 
        int tr[ maxNode ][ maxLen ] ; 
        bool end[ maxNode ] ; 
        int count[ maxNode ] ; 
        int _cnt ; 
        tireTree ( ) {
            _cnt = 1 ; 
            memset ( tr , 0 , sizeof ( tr ) ) ;
            memset ( end , false , sizeof ( end ) ) ;
        }
        public :
            void insert ( char str[ ] ) {
                int cur = 1 , len = strlen ( str + 1 ) ;
                for ( int i=1 ; i<=len ; ++i ) {
                    int idx = str[ i ] - 'a' ; 
                    if ( !tr[ cur ][ idx ] ) {
                        tr[ cur ][ idx ] = ++_cnt ; 
                    }
                    cur = tr[ cur ][ idx ] ; 
                }
                end[ cur ] = true ; 
            }
            
        public :
            bool exist ( char str[ ] ) {
                int cur = 1 , len = strlen ( str + 1 ) ;
                for ( int i=1 ; i<=len ; ++i ) {
                    int idx = str[ i ] - 'a' ; 
                    if ( !tr[ cur ][ idx ] ) {
                        return false ; 
                    }
                    cur = tr[ cur ][ idx ] ; 
                }
                return end[ cur ] ; 
            }
    };
    tireTree tree ; 
    char str[ 2000 ] ;
    int main ( ) {
        int N , M ; 
        scanf ( "%d" , &N ) ; 
        for ( int i=1 ; i<=N ; ++i ) {
            scanf ( "%s" , str + 1 ) ; 
            tree.insert ( str ) ; 
        }
        scanf ( "%d" , &M ) ; 
        for ( int i=1 ; i<=M ; ++i ) {
            scanf ( "%s" , str + 1 ) ; 
            if ( tree.exist ( str ) ) 
                printf ( "YES
    " ) ;
            else 
                printf ( "NO
    " ) ;
        }
        return 0 ; 
    }
    静态字典树

    单调栈

    #include <iostream>
    #include <stack>
    #include <cstdio>
    
    using namespace std ; 
    const int INF = 2147483647 ; 
    
    class Element {
        private :
            int Val , Index ; 
        public :
            Element ( ) {
                Val = 0 ;
                Index = -1 ; 
            }
            Element ( const int __x , const int __y ) {
                Val = __x ; 
                Index = __y ; 
            }
            int getVal ( ) {
                return Val ; 
            }
            int getIndex ( ) {
                return Index ; 
            }
            void print ( ) {
                if ( Index == -1 ) cout << "Do not exist!!" << endl ; 
                else cout << Index << " " << Val << endl ; 
            }
    };
    
    stack <Element> stk ; 
    int arr[ 100 ] ; 
    Element Ans[ 100 ] ; 
    
    int In ( ) {
        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 << 1 ) + ( x << 3 ) + ch - '0' ; ch = getchar ( ) ;}
        return x * f ;  
    }
    
    int main ( ) {
        int N = In ( ) ; 
        
        for ( int i=1 ; i<=N ; ++i ) arr[ i ] = In ( ) ; 
        
        for ( int i=1 ; i<=N+1 ; ++i ) {
            Element ele ;
            if ( i != N+1 ) ele = Element ( arr[i] , i ) ;
            else ele = Element ( INF , -1 ) ;
            while ( !stk.empty ( ) && stk.top().getVal( ) < ele.getVal() ) {
                Ans[ stk.top().getIndex() ] = ele ;
                stk.pop ( ) ; 
            }
            stk.push(ele) ; 
        }
        for ( int i=1 ; i<=N ; ++i ) Ans[ i ].print ( ) ; 
        return 0 ;
    } 
    View Code

     

     

  • 相关阅读:
    开始熟悉一下数据结构了
    首个概率dp
    十六进制转化为八进制
    蓝桥之 剪格子
    蓝桥之 颠倒的价格
    还是闲的
    也是闲的
    闲的无聊,,,
    函数进阶
    函数基础
  • 原文地址:https://www.cnblogs.com/shadowland/p/9859099.html
Copyright © 2020-2023  润新知