[HAOI2007]理想的正方形 st表 || 单调队列

时间:2021-07-17 13:36:47

~~~题面~~~

题解:

  因为数据范围不大,而且题目要求的是正方形,所以这道题有2种解法。

  1,st表。

    这种解法暴力好写好理解,但是较慢。我们设st[i][j][k]表示以(i, j)为左端点,向下/向右分别扩展$2^k$格的最大值,最小值同理,处理完后$n^2$枚举左端点取最优值即可。

    (此为早期代码,写丑了不要介意)

 #include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 1010
#define ac 110
//#define getchar() *S ++
//char READ[1250000],*S = READ;
int n,a,b,ans = INT_MAX;
int st_max[AC][AC][], st_min[AC][AC][];
int k, q = ;
//二维ST表emmmm inline int read()
{
int x = ;char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} inline int Max(int a, int b, int c, int d)
{
if(a > b && a > c && a > d) return a;
else if(b > c && b > d) return b;
else if(c > d) return c;
else return d;
} inline int Min(int a, int b, int c, int d)
{
if(a < b && a < c && a < d) return a;
else if(b < c && b < d) return b;
else if(c < d) return c;
else return d;
} void pre()
{
a = read(), b = read(), n = read();
for(R i = ; i <= a; i ++)
for(R j = ; j <= b; j ++)
st_max[i][j][] = st_min[i][j][] = read();
} void check()
{
for(R i = ; i <= a; i ++)
for(R j = ; j <= b; j ++)
{
printf("!!!(%d , %d)\nst_max:\n", i, j);
for(R l = ; l <= k; l ++)
printf("2^%d = %d\n", l, st_max[i][j][l]);
printf("\n");
printf("st_min:\n");
for(R l = ; l <= k; l ++)
printf("2^%d = %d\n", l, st_min[i][j][l]);
printf("\n\n");
}
} void build()
{
while(n > q) q <<= , ++ k;
-- k, q >>= ;
int pos=;
for(R l = ; l <= k; l ++)
{
for(R i = pos + ; i <= a; i ++)
{
for(R j = pos + ; j <= b; j ++)
{
st_max[i][j][l] = Max(st_max[i - pos][j][l - ], st_max[i][j - pos][l - ], st_max[i - pos][j - pos][l - ], st_max[i][j][l - ]);
st_min[i][j][l] = Min(st_min[i - pos][j][l - ], st_min[i][j - pos][l - ], st_min[i - pos][j - pos][l - ], st_min[i][j][l - ]);
}
}
pos <<= ;
}
} void work()
{
int maxn, minn;
for(R i = n; i <= a; i ++)
for(R j = n; j <= b; j ++)
{
maxn = Max(st_max[i][j][k], st_max[i - n + q][j - n + q][k], st_max[i - n + q][j][k], st_max[i][j - n + q][k]);
minn = Min(st_min[i][j][k], st_min[i - n + q][j - n + q][k], st_min[i - n + q][j][k], st_min[i][j - n + q][k]);
ans = min(ans, maxn - minn);
}
printf("%d\n", ans);
} int main()
{
// freopen("in.in", "r", stdin);
//fread(READ, 1, 1200000, stdin);
pre();
build();
//check();
work();
// fclose(stdin);
return ;
}

  2,单调队列。

    其实也好理解,,,但是感觉很多博客没有图所以意思讲的不是很清晰,这里就详细讲一下吧。

    类似于滑动窗口,如果没做过这题建议先理解这题的做法。

    可以看做此题就是滑动窗口的二维扩展版。那么我们已经有了在序列上获取指定区间大小的最大最小值的方法,要如何才能扩展到二维平面上呢?

    其实画个图就很好理解了。

[HAOI2007]理想的正方形 st表 || 单调队列

    如果我们将每个红色区间的最大最小值都存在蓝色点上,那么只需要对蓝色点做一次滑动窗口,就可以获得指定大小的矩形最大最小值了。

    因为每个蓝色点已经代表了指定区间大小的行的最大最小值,所以再在这个基础上查询蓝点指定区间的最大最小值就相当于是在查询一个矩形了。

    

 #include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 1100
#define LL long long int n, m, k, ans = INT_MAX;
int s[AC][AC], g[AC][AC], f[AC][AC]; struct node{
int x, id;
}; struct que{
node q[AC];int head, tail;
void init()
{
head = , tail = ;
} void add_max(int x, int id)
{
while(head <= tail && q[head].id <= id - k) ++ head;
while(head <= tail && q[tail].x <= x) -- tail;
q[++tail] = (node){x, id};
} void add_min(int x, int id)
{
while(head <= tail && q[head].id <= id - k) ++ head;
while(head <= tail && q[tail].x >= x) -- tail;
q[++tail] = (node){x, id};
} int top() {return q[head].x;}
}q1, q2; inline void upmin(int &a, int b)
{
if(b < a) a = b;
} inline int read()
{
int x = ;char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} void pre()
{
n = read(), m = read(), k = read();
for(R i = ; i <= n; i ++)
for(R j = ; j <= m; j ++) s[i][j] = read();
} void build()//先对每一行求出来
{
for(R i = ; i <= n; i ++)//枚举行
{
q1.init(), q2.init();
for(R j = ; j <= m; j ++)//枚举列
{
q1.add_min(s[i][j], j), q2.add_max(s[i][j], j);
if(j >= k) f[i][j] = q1.top(), g[i][j] = q2.top();
}
}
} void work()//再求整体的
{
for(R i = k; i <= m; i ++)//先枚举列,再枚举行
{
q1.init(), q2.init();
for(R j = ; j <= n; j ++)
{
q1.add_min(f[j][i], j), q2.add_max(g[j][i], j);
if(j >= k) upmin(ans, q2.top() - q1.top());
}
}
printf("%d\n", ans);
} int main()
{
// freopen("in.in", "r", stdin);
pre();
build();
work();
// fclose(stdin);
return ;
}