BZOJ 1567: [JSOI2008]Blue Mary的战役地图
考虑二分答案暴力 $n^4$ 枚举两个矩形右下角,如果能做到 $O(1)$ 判断那么复杂度就可行
那么容易想到二维哈希,然后直接搞就好了...
二维哈希怎么做还是看代码吧...
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; typedef unsigned long long ull; inline int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*f; } const int N=57,base1=9924353,base2=1e9+7; int n,ans; ull ha1[N][N],ha2[N][N],hb1[N][N],hb2[N][N],fac1[N],fac2[N]; inline ull get_h(int x,int y,int l,bool flag) { if(flag) return ha2[x][y]-ha2[x][y-l]*fac1[l]-ha2[x-l][y]*fac2[l]+ha2[x-l][y-l]*fac1[l]*fac2[l]; return hb2[x][y]-hb2[x][y-l]*fac1[l]-hb2[x-l][y]*fac2[l]+hb2[x-l][y-l]*fac1[l]*fac2[l]; } inline bool check(int p) { for(int i=p;i<=n;i++) for(int j=p;j<=n;j++) for(int k=p;k<=n;k++) for(int l=p;l<=n;l++) if(get_h(i,j,p,1)==get_h(k,l,p,0)) return 1; return 0; } int main() { n=read(); fac1[0]=fac2[0]=1; for(int i=1;i<=n;i++) fac1[i]=fac1[i-1]*base1,fac2[i]=fac2[i-1]*base2; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) ha1[i][j]=ha1[i][j-1]*base1+read(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) hb1[i][j]=hb1[i][j-1]*base1+read(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { ha2[i][j]=ha2[i-1][j]*base2+ha1[i][j], hb2[i][j]=hb2[i-1][j]*base2+hb1[i][j]; } int L=1,R=n; while(L<=R) { int mid=L+R>>1; if(check(mid)) L=mid+1,ans=mid; else R=mid-1; } printf("%d\n",ans); return 0; }