洛谷P1169 [ZJOI2007]棋盘制作 悬线法 动态规划

时间:2023-03-08 21:28:02

P1169 [ZJOI2007]棋盘制作

(逼着自己做DP

题意:

  给定一个包含0,1的矩阵,求出一个面积最大的正方形矩阵和长方形矩阵,要求矩阵中相邻两个的值不同。

思路:

  悬线法。

  用途:

    解决给定矩阵中满足条件的最大子矩阵

  做法:

    用一条线(横竖貌似都行)左右移动直到不满足约束条件或者到达边界

  定义几个东西:

    left[i][j]left[i][j]:代表从(i,j)(i,j)能到达的最左位置

    right[i][j]right[i][j]:代表从(i,j)(i,j)能到达的最右位置

    up[i][j]up[i][j]:代表从(i,j)(i,j)向上扩展最长长度.

  递推公式:

    left[i][j]=max(left[i][j],left[i-1][j]left[i][j]=max(left[i][j],left[i−1][j]

    right[i][j]=min(right[i][j],right[i-1][j]right[i][j]=min(right[i][j],right[i−1][j]

  至于为什么递推公式中考虑上一层的情况?

    是因为up数组的定义,up数组代表向上扩展最长长度, 所以需要考虑上一层的情况.

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = ;
int mp[maxn][maxn];
int lef[maxn][maxn],righ[maxn][maxn],up[maxn][maxn];
int main(){
int n,m;
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++){
for(int j=; j<=m; j++) {
scanf("%d", &mp[i][j]);
}
} for(int i=; i<=n; i++){
for(int j=; j<=m; j++){
lef[i][j] = j;
if(j> && mp[i][j] != mp[i][j-]) lef[i][j] = lef[i][j-];
up[i][j] = ;
}
} for(int i=; i<=n; i++){
for(int j=m; j>=; j--){
righ[i][j] = j;
if(j < m && mp[i][j] != mp[i][j+]) righ[i][j] = righ[i][j+];
}
}
int ans1 = , ans2 = ;
for(int i=; i<=n; i++){
for(int j=; j<=m; j++){
if(mp[i][j] != mp[i-][j]){
lef[i][j] = max(lef[i][j] , lef[i-][j]);
righ[i][j] = min(righ[i][j], righ[i-][j]);
up[i][j] = up[i-][j] + ;
} int a = righ[i][j] - lef[i][j] + ;
int b = up[i][j];
// cout<<a<<" "<<b<<endl;
ans2 = max(a * b, ans2); int t = min(a, b);
ans1 = max(t * t, ans1);
}
}
printf("%d\n%d\n", ans1, ans2);
return ;
}