Cod sursa(job #2483113)

Utilizator PetyAlexandru Peticaru Pety Data 29 octombrie 2019 12:38:57
Problema DreptPal Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, p[1002][1002], v[1002][1002], m, a[1002], st[1002], dr[1002], ans;


int main()
{
  fin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      fin >> v[i][j];
  for (int i = 1; i <= n; i++) {
    int l = 0, r = 0;
    int t = 0;
    for (int j = 1; j <= m; j++) {
      if (j > r)
        t = 0;
      else
        t = min(r - j, p[i][l + r - j]);
      while (j + t <= m && j >= t && v[i][j - t] == v[i][j + t])
        t++;
      p[i][j] = t;
      if (j + t > r) {
        l = j - t;
        r = j + t;
      }
    }
  }
  for (int j = 1; j <= m; j++) {
    for (int i = 1; i <= n; i++)
      a[i] = p[i][j] * 2 - 1;
    stack<int>s;
    s.push(0);
    for (int i = 1; i <= n; i++) {
      while (!s.empty() && a[s.top()] >= a[i])
        s.pop();
      st[i] = s.top();
      s.push(i);
    }
    while (!s.empty())
      s.pop();
    s.push(n + 1);
    for (int i = n; i >= 1; i--) {
      while (!s.empty() && a[s.top()] >= a[i])
        s.pop();
      dr[i] = s.top();
      s.push(i);
    }
    for (int i = 1; i <= n; i++) {
      ans = max(ans, a[i] * (dr[i] - st[i] - 1));
    }
  }fout << ans;
  return 0;
}