Cod sursa(job #638039)

Utilizator PetcuIoanPetcu Ioan Vlad PetcuIoan Data 20 noiembrie 2011 18:18:46
Problema DreptPal Scor 50
Compilator cpp Status done
Runda .com 2011 Marime 2.29 kb
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<algorithm>
using namespace std;

int n,m,sol,meniu,st1[1012],st2[1012],li[1012],ri[1012],v[1010];
char pars[30000];
int p[1011][1020];

void do_smen(int x);

void read()
{
    assert(freopen("dreptpal.in","r",stdin)!=NULL);
    scanf("%d%d\n",&n,&m);
    int i,j,u,h;
    int d;
    for(i=1;i<=n;++i)
    {
        j=1;
        gets(pars);
        u=strlen(pars);
        h=0;
        d=0;
        while(h<u)
        {
            if(pars[h]==' ')
            {
                v[j]=d;
                ++j;
                d=0;
                ++h;
                continue;
            }
            d*=10;
            d+=pars[h]-'0';
            ++h;
        }
        v[j]=d;
        do_smen(i);
    }
}

void do_smen(int x)
{
    int st,dr,j,last,lc;
    for(j=1;j<=m;++j)
    {
        if(j==1)
        {
            p[x][j]=1;
            lc=1;
            last=1;
        }
        else
        {
            p[x][j]=1;
            if(last>=j)
                p[x][j]=max(1,p[x][lc*2-j]);
            st=j-(p[x][j]+1)/2;
            dr=j+(1+p[x][j])/2;
            while(st>0 && dr<=m && v[st]==v[dr])
            {
                p[x][j]+=2;
                ++dr;
                --st;
            }
            ++st;
            --dr;
            if(dr>last)
            {
                lc=j;
                last=dr;
            }
        }
    }
}

void solve()
{
    int i,j;
    for(i=1;i<=m;++i)
    {
        for(j=1;j<=n;++j)
        {
            while(st1[0] && p[st1[st1[0]]][i]>=p[j][i])
                {
                    --st1[0];
                }
            li[j]=j-st1[st1[0]]-1;
            st1[++st1[0]]=j;
        }
        for(j=n;j>0;--j)
        {
            while(st2[0] && p[st2[st2[0]]][i]>=p[j][i])
            {
                --st2[0];
            }
            if(st2[0]==0)
                ri[j]=n-j;
            else
                ri[j]=st2[st2[0]]-j-1;
            st2[++st2[0]]=j;
        }
        for(j=1;j<=n;++j)
            sol=max(sol,p[j][i]*(ri[j]+li[j]+1));
        st2[0]=0;
        st1[0]=0;
    }
    meniu=sol;
    if(meniu<n)
        meniu=n;
}

void write()
{
    assert(freopen("dreptpal.out","w",stdout)!=NULL);
    printf("%d",meniu);
}

int main()
{
    read();
    solve();
    write();
    return 0;
}