# bzoj1047理想的正方形

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a,b,n;
int map[1010][1010];
int ans=0x3f3f3f3f;
int main()
{
scanf("%d%d%d",&a,&b,&n);
for(int i=1;i<=a;i++)
for(int j=1;j<=b;j++)
scanf("%d",&map[i][j]);
for(int i=1;i+n-1<=a;i++)
for(int j=1;j+n-1<=b;j++){
int maxx=0,minn=0x3f3f3f3f;
for(int l=i;l<=n+i-1;l++)
for(int r=j;r<=n+j-1;r++)
maxx=max(maxx,map[l][r]),
minn=min(minn,map[l][r]);
ans=min(ans,maxx-minn);
}
printf("%d",ans);
return 20020902;
}

($ps:$从$(1,1)$开始计数)

$$maxx[i][j][k]=max(max(maxx[i][j][k-1],maxx[i+1][j+1][k-1]),max(maxx[i][j+1][k-1],maxx[i+1][j][k-1]))$$
$$minn[i][j][k]=min(min(minn[i][j][k-1],minn[i+1][j+1][k-1]),min(minn[i][j+1][k-1],minn[i+1][j][k-1]))$$

// luogu-judger-enable-o2
#pragma GCC optimize (2)
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a,b,n;
int map[1010][1010];
int maxx[1010][1010];
int minn[1010][1010];
int ans=0x3f3f3f3f;
int main()
{
scanf("%d%d%d",&a,&b,&n);
for(int i=1;i<=a;i++)
for(int j=1;j<=b;j++)
scanf("%d",&map[i][j]),
maxx[i][j]=minn[i][j]=map[i][j];
for(int k=2;k<=n;k++)
for(int i=1;i+k<=a+1;i++)
for(int j=1;j+k<=b+1;j++)
maxx[i][j]=max(max(maxx[i][j],maxx[i+1][j+1]),max(maxx[i][j+1],maxx[i+1][j])),
minn[i][j]=min(min(minn[i][j],minn[i+1][j+1]),min(minn[i][j+1],minn[i+1][j])),
ans=k==n?min(ans,maxx[i][j]-minn[i][j]):0x3f3f3f3f;
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
*/

posted @ 2018-08-03 10:53  米罗偕涯  阅读(383)  评论(0编辑  收藏  举报