# [二分 ST表] HihoCoder #1469 Challenge 27 福字

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;

inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
}
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

const int N=1005;
const int K=12;

struct ST{
int st[N][K],Log[N];
inline void Pre(int *a,int n){
for (int i=2;i<=n;i++) Log[i]=Log[i>>1]+1;
for (int i=1;i<=n;i++) st[i][0]=a[i];
for (int k=1;k<K;k++)
for (int i=1;i<=n;i++)
{
st[i][k]=st[i][k-1];
if (i+(1<<(k-1))<=n)
st[i][k]=min(st[i][k],st[i+(1<<(k-1))][k-1]);
}
}
inline int Query(int l,int r){
int t=Log[r-l+1];
return min(st[l][t],st[r-(1<<t)+1][t]);
}
}B[N],C[N];

inline bool check(int x,int y,int k){
return C[x].Query(y,y+k-1)>=k && B[y].Query(x,x+k-1)>=k;
}

int n;
int a[N][N];
int b[N][N],c[N][N];

int main(){
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) read(a[i][j]);
for (int i=1;i<=n;i++){
b[i][n]=1; int cnt=1;
for (int j=n-1;j;j--){
if (a[i][j]==a[i][j+1]-1)
cnt++;
else
cnt=1;
b[i][j]=cnt;
}
}
for (int i=1;i<=n;i++)
for (int j=1;j<i;j++) swap(b[i][j],b[j][i]);
for (int j=1;j<=n;j++){
c[n][j]=1; int cnt=1;
for (int i=n-1;i;i--){
if (a[i][j]==a[i+1][j]-1)
cnt++;
else
cnt=1;
c[i][j]=cnt;
}
}
int ans=0;
for (int i=1;i<=n;i++)
B[i].Pre(b[i],n),C[i].Pre(c[i],n);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++){
int L=1,R=min(n-i+1,n-j+1)+1,MID;
while (L+1<R)
if (check(i,j,MID=(L+R)>>1))
L=MID;
else
R=MID;
ans=max(ans,L);
}
printf("%d\n",ans);
return 0;
}


• 评论

4

• 上一篇
• 下一篇