K. Largest Common Submatrix 2019ICPC银川现场赛

it2026-06-15  8

https://nanti.jisuanke.com/t/42391

去年思想出问题了这题没做出来我吐了

当时没注意到两个矩阵的数字都是1-n*m的排列,注意到这个那么相同的子矩阵就是对应数字的横坐标和纵坐标的差值相同就行了

然后就是个傻逼最大全零子矩阵

#include<bits/stdc++.h> using namespace std; const int maxl=1010; int n,m,top,ans; int h[maxl],s[maxl],l[maxl],r[maxl]; int a[maxl][maxl],b[maxl][maxl]; int dx[maxl][maxl],dy[maxl][maxl]; struct pos { int x,y; }ina[maxl*maxl]; inline void prework() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&a[i][j]),ina[a[i][j]]=pos{i,j}; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf("%d",&b[i][j]); dx[i][j]=i-ina[b[i][j]].x; dy[i][j]=j-ina[b[i][j]].y; } } inline bool pd(int i,int j,int x,int y) { if(x==0 || y==0 || x>n || y>m) return false; return dx[i][j]==dx[x][y] && dy[i][j]==dy[x][y]; } inline void mainwork() { ans=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) if(pd(i,j,i-1,j)) h[j]=h[j]+1; else h[j]=1; top=0;s[0]=0; for(int j=1;j<=m;j++) { if(!pd(i,j,i,j-1)) s[0]=j-1,top=0; while(top>0 && h[j]<=h[s[top]]) top--; l[j]=s[top]+1; s[++top]=j; } s[0]=m+1;top=0; for(int j=m;j>=1;j--) { if(!pd(i,j,i,j+1)) s[0]=j+1,top=0; while(top>0 && h[j]<=h[s[top]]) top--; r[j]=s[top]-1; s[++top]=j; ans=max(ans,h[j]*(r[j]-l[j]+1)); } } } inline void print() { printf("%d\n",ans); } int main() { prework(); mainwork(); print(); return 0; }

 

最新回复(0)