Fork me on GitHub

TLS 9.2 C

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$就行了.

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

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

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

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

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

$Code:$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#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 ;
}