zoukankan      html  css  js  c++  java
  • TLS 9.2C

    TLS 9.2C

    这个题目我觉得我做的(50)分做法比(100)分的(SBDP)更具有价值.

    因为这个(DP)真的很简单.

    (f_{i,j})表示以((i,j))为右下角的最大正方形的边长.则有转移方程:

    [f_{i,j} = min ( f_{i-1,j-1} , f_{i-1,j} , f_{i,j-1} ) + 1 ]

    最后的答案就在所有的(f_{i,j})中取一个(max)就行了.

    (Code:)

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <queue>
    #include <cmath>
    #include <ctime>
    #include <map>
    #include <set>
    #define MEM(x,y) memset ( x , y , sizeof ( x ) )
    #define rep(i,a,b) for (int i = a ; i <= b ; ++ i)
    #define per(i,a,b) for (int i = a ; i >= b ; -- i)
    #define pii pair < int , int >
    #define X first
    #define Y second
    #define rint read<int>
    #define int long long
    #define pb push_back
    
    using std::set ;
    using std::pair ;
    using std::max ;
    using std::min ;
    using std::priority_queue ;
    using std::vector ;
    
    template < class T >
        inline T read () {
            T 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 << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
                ch = getchar () ;
           }
       return f * x ;
    }
    
    const int N = 3e3 + 100 ;
    
    int n , m , f[N][N] , ans ;
    bool e[N][N] ;
    
    signed main (int argc , char * argv[] ) {
        freopen ("photo.in" , "r" , stdin) ;
        freopen ("photo.out" , "w" , stdout) ;
        n = rint () ; m = rint () ;
        rep ( i , 1 , m ) e[rint()][rint()] = true ;
        rep ( i , 1 , n ) rep ( j , 1 , n )
            if ( ! e[i][j] ) f[i][j] = min ( f[i-1][j-1] , min ( f[i-1][j] , f[i][j-1] ) ) + 1 ;
            else f[i][j] = 0 ;
        rep ( i , 1 , n ) rep ( j , 1 , n ) ans = max ( ans , f[i][j] ) ;
        printf ("%lld
    " , ans ) ; return 0 ;
    }
    

    这是(O(n^2))(DP)做法.

    我接下里要说的这个(O(n^3))的暴力做法其实是非常优美的.

    它其实是悬线法求极大正方形的基础,这里我只维护了一个向右的最远扩展距离.

    这个数组可以(O(n^3))预处理,但其实可以做到(O(n^2)),从最右边向左递推就可以做到(O(n^2)).

    这样借助这个数组,我们就可以枚举这个极大正方形的左右边界,然后从上向下去枚举,保证必须要连续一段都大于等于这个枚举出来的边长(枚举左右边界就知道了边长),每次遇到合法的就取(max)就可以了.

    (Code:)

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <queue>
    #include <cmath>
    #include <ctime>
    #include <map>
    #include <set>
    #define MEM(x,y) memset ( x , y , sizeof ( x ) )
    #define rep(i,a,b) for (int i = a ; i <= b ; ++ i)
    #define per(i,a,b) for (int i = a ; i >= b ; -- i)
    #define pii pair < int , int >
    #define X first
    #define Y second
    #define rint read<int>
    #define int long long
    #define pb push_back
    
    using std::set ;
    using std::pair ;
    using std::max ;
    using std::min ;
    using std::priority_queue ;
    using std::vector ;
    
    template < class T >
        inline T read () {
            T 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 << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
                ch = getchar () ;
           }
       return f * x ;
    }
    
    const int N = 3e3 + 100 ;
    
    int n , m , r[N][N] , ans ;
    bool e[N][N] ;
    
    signed main (int argc , char * argv[] ) {
        freopen ("photo.in" , "r" , stdin) ;
        freopen ("photo.out" , "w" , stdout) ;
        n = rint () ; m = rint () ;
        rep ( i , 1 , m ) e[rint()][rint()] = true ;
        rep ( i , 1 , n ) rep ( j , 1 , n ) rep ( k , j , n )
            if ( ! e[i][k] ) ++ r[i][j] ; else break ;
        rep ( i , 1 , n ) rep ( j , i , n ) {
            int last = 0 , len = j - i + 1 ;
            rep ( k , 1 , n ) {
                if ( ! last ) {
                    if ( r[k][i] >= len ) last = k ;
                    else last = 0 ;
                } else {
                    if ( r[k][i] >= len ) {
                        if ( k - last + 1 == len ) ans = max ( ans , len ) ;
                    } else last = 0 ;
                }
            }
        }
        printf ("%lld
    " , ans ) ;
        system ("pause") ; return 0 ;
    }
    
    May you return with a young heart after years of fighting.
  • 相关阅读:
    vue技术分享之你可能不知道的7个秘密
    JVM知识总结-运行时区域划分
    如何使用加多宝(jdb)在linux下调试Java程序
    RabbitMQ 高可用之镜像队列
    Gson格式转换Integer变为Double类型问题解决
    IPv6地址表示方式
    MySQL truncate()函数的使用说明
    Java 实现判断 主机是否能 ping 通
    MySQL 性能优化系列之一 单表预处理
    Linux 查看CPU和内存的使用情况
  • 原文地址:https://www.cnblogs.com/Equinox-Flower/p/11448050.html
Copyright © 2011-2022 走看看