其他分享
首页 > 其他分享> > 洛谷P2216 理想的正方形

洛谷P2216 理想的正方形

作者:互联网

题目
有可以直接找\(maxn[i][j][t]\)为\((i,j)\)为左下角长度为t的正方形内的最大值。
然后可以有以下转移:

\(maxn[i][j][t] = Max(maxn[i + 1][j][t-1], maxn[i][j][t-1], maxn[i][j + 1][t-1], maxn[i + 1][j + 1][t-1]);\)

然后考虑滚动数组,最后答案直接枚举即可。

#include <bits/stdc++.h>
#define N 1011
using namespace std;
int n, m, k, ans = 2147483647;
int a[N][N], sum[N][N], minn[N][N], maxn[N][N];//maxn[i][j]表示以i,j为左下角的矩阵最大值。 
int Max(int a, int b, int c, int d)
{
    return max(max(a, b), max(c, d));
}
int Min(int a, int b, int c, int d)
{
    return min(min(a, b), min(c, d));   
}
inline void init()
{ //maxn[i][j][k]以i,j为左下角的长度为(1 << k)的矩阵最大值
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            scanf("%d", &a[i][j]);
            maxn[i][j] = a[i][j], minn[i][j] = a[i][j];
        }
    for (int t = 2; t <= k; t++)//t是区间长度
        for (int i = 1; i + 1 <= n; i++)
            for (int j = 1; j + 1 <= m; j++)
            {
                maxn[i][j] = Max(maxn[i + 1][j], maxn[i][j], maxn[i][j + 1], maxn[i + 1][j + 1]);
                minn[i][j] = Min(minn[i + 1][j], minn[i][j], minn[i][j + 1], minn[i + 1][j + 1]);
            }
}
int main()
{
//  freopen("ha.in", "r", stdin);
    init();
    for (int i = 1; i + k - 1 <= n; i++)
        for (int j = 1; j + k - 1 <= m; j++)
            ans = min(ans, maxn[i][j] - minn[i][j]);
    printf("%d", ans);
    return 0;
}
/*
5 4 2
1 2 5 6
0 17 16 0
16 17 2 1
2 10 2 1
1 2 2 2
*/

标签:洛谷,min,int,max,正方形,maxn,Max,P2216,左下角
来源: https://www.cnblogs.com/liuwenyao/p/11757883.html