Cod sursa(job #2857454)

Utilizator puica2018Puica Andrei puica2018 Data 25 februarie 2022 17:29:17
Problema DreptPal Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("dreptpal.in");
ofstream fout("dreptpal.out");

int n,m;
int a[1005][1005],p[1005][1005],up[1005][1005],down[1005][1005];
stack <int> st;

int main()
{
    fin>>n>>m;
    for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) fin>>a[i][j];
    for(int i=1; i<=n; i++)
    {
        int c=1,r=1;
        for(int j=2; j<=m; j++)
        {
            if(r>=j) p[i][j]=min(p[i][2*c-j],r-j);
            while(j+p[i][j]+1 && j>p[i][j]+1 && a[i][j+p[i][j]+1]==a[i][j-p[i][j]-1])  p[i][j]++;
            if(j+p[i][j]>r)
            {
                r=j+p[i][j];
                c=j;
            }
        }
    }
    for(int j=1; j<=m; j++)
    {
        for(int i=1; i<=n; i++)
        {
            while(!st.empty() && p[st.top()][j]>=p[i][j]) st.pop();
            if(st.empty())
                up[i][j]=i-1;
            else
                up[i][j]=i-st.top()-1;
            st.push(i);
        }
        while(!st.empty()) st.pop();
        for(int i=n; i>=1; i--)
        {
            while(!st.empty() && p[st.top()][j]>=p[i][j]) st.pop();
            if(st.empty())
                down[i][j]=n-i;
            else
                down[i][j]=st.top()-i-1;
            st.push(i);
        }
    }
    int maxim=0;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            maxim=max(maxim,(2*p[i][j]+1)*(up[i][j]+down[i][j]+1));
    fout<<maxim<<"\n";
    return 0;
}