Cod sursa(job #1004815)

Utilizator sleepaholicNeculaescu Theodor sleepaholic Data 3 octombrie 2013 18:03:06
Problema DreptPal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include<cstdio>
using namespace std;
int maxi,nr,i,j,n,m,a[1009][1009],b[1009][1009],st[1009],S[1009],J[1009];
int min(int a,int b)
{
    if(a<b) return a;
    return b;
}
void pscpld(int i)
{
    int P,j,R=0;
    for(j=1;j<=m;j++)
    {
        b[i][j]=1;
        if(R>j) b[i][j]=min(b[i][P-(j-P)],R-j+1);
        while(a[i][j-b[i][j]]==a[i][j+b[i][j]]&&j+b[i][j]<=m&&j-b[i][j]>=1) b[i][j]++;
        if(j+b[i][j]-1>R)
        {
            R=j+b[i][j]-1;
            P=j;
        }
    }
}
int main()
{
freopen("dreptpal.in","r",stdin);
freopen("dreptpal.out","w",stdout);
scanf("%d",&n);
scanf("%d",&m);
for(i=1;i<=n;i++)
{
    for(j=1;j<=m;j++)
        scanf("%d",&a[i][j]);
    pscpld(i);
}
for(j=1;j<=m;j++)
{
    nr=0;
    st[0]=0;
    for(i=1;i<=n;i++)
    {
        while(nr&&b[st[nr]][j]>=b[i][j]) nr--;
        S[i]=i-st[nr];
        nr++;
        st[nr]=i;
    }
    nr=0;
    st[0]=n+1;
    for(i=n;i>=1;i--)
    {
        while(nr&&b[st[nr]][j]>=b[i][j]) nr--;
        J[i]=st[nr]-i;
        nr++;
        st[nr]=i;
    }
    for(i=1;i<=n;i++)
        if((2*b[i][j]-1)*(S[i]+J[i]-1)>maxi) maxi=(2*b[i][j]-1)*(S[i]+J[i]-1);
}
printf("%d\n",maxi);
return 0;
}