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\n" , 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\n" , ans ) ;
system ("pause") ; return 0 ;
}
May you return with a young heart after years of fighting.