## BZOJ 1047: [HAOI2007]理想的正方形

###### 题目

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXab=1000+7,MAXn=100+7,inf=2147483647;
typedef int MATRIX[MAXn][MAXab][MAXab];
MATRIX fmin,fmax;
inline int max(int a,int b){return a>b?a:b;}
inline int min(int a,int b){return a>b?b:a;}
inline int min4(int a,int b,int c,int d){return min(min(min(a,b),c),d);}
inline int max4(int a,int b,int c,int d){return max(max(max(a,b),c),d);}

int main(){
//freopen("b1047.in","r",stdin);
int a,b,n;
scanf("%d%d%d",&a,&b,&n);
for (int i=1;i<=a;i++)
for (int j=1;j<=b;j++) {
scanf("%d",&fmin[1][i][j]);
fmax[1][i][j]=fmin[1][i][j];
}
int ans=inf;
for (int k=2;k<=n;k++){         //kは决策です！
for (int i=1;i<=a-k+1;i++)
for (int j=1;j<=b-k+1;j++){
fmin[k][i][j]=
min4(fmin[k-1][i][j],fmin[k-1][i+1][j],
fmin[k-1][i][j+1],fmin[k-1][i+1][j+1]);
fmax[k][i][j]=
max4(fmax[k-1][i][j],fmax[k-1][i+1][j],
fmax[k-1][i][j+1],fmax[k-1][i+1][j+1]);
if (k==n) ans=min(fmax[k][i][j]-fmin[k][i][j],ans);
}
}
printf("%d",ans);
return 0;
}

/*优先队列！*/
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXn=100+7,MAXab=1000+7;
const int inf=0x7fffffff;
int n,a,b,fmin[MAXab][MAXab],fmax[MAXab][MAXab];

inline int max(int a,int b){return a>b?a:b;}
inline int min(int a,int b){return a<b?a:b;}

int main(){
//	freopen("b1047.in","r",stdin);
//	freopen("b1047.out","w",stdout);
scanf("%d%d%d",&a,&b,&n);
for (int i=1;i<=a;i++){
int ts,ws,tb,wb,qs[MAXab]={0},qb[MAXab]={-inf},v[MAXab]={0};
ts=ws=tb=wb=1;
for (int j=1;j<=b;j++){
scanf("%d",&v[j]);
while (ws-->ts&&v[j]<v[qs[ws]]);ws++;qs[ws++]=j;
if (qs[ws-1]-qs[ts]+1>n) ts++;
while (wb-->tb&&v[j]>v[qb[wb]]);wb++;qb[wb++]=j;
if (qb[wb-1]-qb[tb]+1>n) tb++;
if (j>=n) fmin[i][j-n+1]=v[qs[ts]],fmax[i][j-n+1]=v[qb[tb]];
}
}

int ans=inf;
for (int i=1;i<=a-n+1;i++){
for (int j=1;j<=b-n+1;j++){
int minans=inf,maxans=-inf;
for (int k=0;k<n;k++){
minans=min(minans,fmin[i+k][j]);
maxans=max(maxans,fmax[i+k][j]);
}
ans=min(ans,maxans-minans);
}
}
cout<<ans<<endl;
return 0;
}


posted on 2016-07-26 07:45  Chuckqgz  阅读(...)  评论(...编辑  收藏