Pagini recente » Cod sursa (job #1301842) | Cod sursa (job #2185282) | Cod sursa (job #1762165) | Cod sursa (job #2834251) | Cod sursa (job #637393)
Cod sursa(job #637393)
#include <cstdio>
#define Nmax 1001
#define Mmax 1001
int N, M, i, j, st;
int A[Nmax][Mmax], L[Nmax][Nmax];
int A1[Nmax], A2[Nmax], S[Nmax];
int best;
int palind(int x, int y)
{
int st, dr;
st = dr = x;
while (st>=0 && dr<M)
{
if (A[y][st] != A[y][dr]) break;
--st; ++dr;
}
return dr-st-1;
}
int main()
{
freopen("dreptpal.in","r",stdin);
freopen("dreptpal.out","w",stdout);
scanf("%d %d", &N, &M);
for (i=0; i<N; ++i)
for (j=0; j<M; ++j)
scanf("%d", &A[i][j]);
for (i=0; i<M; ++i)
{
for (j=0; j<N; ++j)
{
L[j][i] = palind(i, j);
if (L[j][i] > best) best = L[j][i];
}
//stanga
for (j=0; j<N; ++j)
{
A1[j] = L[0][i];
while (L[S[st]][i] >= L[j][i] && st>=0)
--st;
if (st>=0 && L[j][i] * (j - S[st]) > A1[j]) A1[j] = L[j][i] * (j - S[st]);
if (st<0 && L[j][i] * (j + 1) > A1[j]) A1[j] = L[j][i] * (j + 1);
S[++st] = j;
}
//dreapta
for (j=N-1; j>=0; --j)
{
A2[j] = L[j][i];
while (L[S[st]][i] >= L[j][i] && st>=0)
--st;
if (st>=0 && L[j][i] * (S[st] - j) > A2[j]) A2[j] = L[j][i] * (S[st] - j);
if (st<0 && L[j][i] * (N - j) > A2[j]) A2[j] = L[j][i] * (N - j);
S[++st] = j;
}
//total
for (j=0; j<N; ++j)
if (A1[j] + A2[j] - L[j][i] > best) best = A1[j] + A2[j] - L[j][i];
}
printf("%d\n", best);
return 0;
}