题目链接
在现场的时候就有往单调栈方向上想(或许是直觉吧),但是最后实在时间不够我们想的了,最后成了银,其实运气也挺好的了,罚时那么高,真的很担心最后会不会又混了个铜首。RP clear了,最后银川的银换来了沈阳的打铁(RP++)。
然后,赛后的时候和队友讨论了一下,然后发现我们可以先预处理每个点所能向右到达的最远距离,然后每次遍历这一整条横轴上的点的横向所能到达,在这里有个细节就是能不能和上面的点再链接,还是需要进行考虑一下的,然后还有一些细节就自己debug一下~还有一件事,可以先把这道题作为练习:Largest Rectangle in a Histogram【POJ 2559】【单调栈】。
样例一:
4 1
1
2
3
4
2
3
1
4
样例二:
4 2
1 5
2 6
3 7
4 8
2 6
3 8
4 7
1 5
ans:3
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e3 + 7;
int N, M, a[maxN][maxN], b[maxN][maxN], h[maxN][maxN] = {0};
struct node
{
int x, y;
node(int a=0, int b=0):x(a), y(b) {}
friend bool operator == (node e1, node e2) { return e1.x == e2.x && e1.y == e2.y; }
}t[maxN * maxN], det[maxN * maxN];
struct MQ
{
int height, depth;
MQ(int a=0, int b=0):height(a), depth(b) {}
}Stap[maxN];
int Stop, ans = 1;
inline void momo_queue(int jj)
{
Stop = 0;
Stap[0] = MQ();
det[b[N + 1][jj]] = node(INF, INF);
bool flag = true;
for(int i=1, S; i<=N + 1; i++)
{
flag = (det[b[i - 1][jj]] == det[b[i][jj]]);
while(Stop && (Stap[Stop].height > h[i][jj] || !flag))
{
S = (i - Stap[Stop - 1].depth - 1) * Stap[Stop].height;
ans = max(ans, S);
Stop--;
}
if(h[i][jj] > Stap[Stop].height)
{
if(!flag) Stap[0].depth = i - 1;
Stap[++Stop] = MQ(h[i][jj], i);
}
else Stap[Stop].depth = i;
}
}
int main()
{
scanf("%d%d", &N, &M);
for(int i=1; i<=N; i++)
{
for(int j=1; j<=M; j++)
{
scanf("%d", &a[i][j]);
t[a[i][j]] = node(i, j);
}
}
for(int i=1; i<=N; i++)
{
for(int j=1; j<=M; j++)
{
scanf("%d", &b[i][j]);
det[b[i][j]] = node(t[b[i][j]].x - i, t[b[i][j]].y - j);
}
}
for(int i=1; i<=N; i++)
{
h[i][M] = 1;
for(int j = M - 1; j >= 1; j--)
{
if(det[b[i][j]] == det[b[i][j + 1]]) h[i][j] = h[i][j + 1] + 1;
else h[i][j] = 1;
}
}
for(int j=1; j<=M; j++)
{
momo_queue(j);
}
printf("%d\n", ans);
return 0;
}