Cod sursa(job #3278556)

Utilizator Carnu_EmilianCarnu Emilian Carnu_Emilian Data 20 februarie 2025 09:20:17
Problema DreptPal Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fcin("dreptpal.in");
ofstream fcout("dreptpal.out");

const int N = 1e3 + 5;
int v[N][N], a[N][N], st[N], dr[N];
int n, m, rasp;

/**
4 5
5 3 4 3 2
6 2 6 2 6
1 3 1 3 4
7 8 3 9 2

3
5
3
1
*/

int main()
{
    fcin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            fcin >> v[i][j];
    for (int i = 1; i <= n; i++)
    {
        v[i][0] = -1;
        v[i][m + 1] = -2;
        for (int j = 1; j <= m; j++)
        {
            int st, dr;
            for (st = j - 1, dr = j + 1; v[i][st] == v[i][dr]; st--, dr++);
            a[i][j] = dr - st - 1;
        }
    }
    for (int j = 1; j <= m; j++)
    {
        stack<int> s;
        for (int i = 1; i <= n; i++)
        {
            while (!s.empty() && a[s.top()][j] >= a[i][j])
                s.pop();
            st[i] = (s.empty()) ? 0 : s.top();
            s.push(i);
        }
        while (!s.empty())
            s.pop();
        for (int i = n; i >= 1; i--)
        {
            while (!s.empty() && a[s.top()][j] >= a[i][j])
                s.pop();
            dr[i] = (s.empty()) ? (n + 1) : s.top();
            s.push(i);
        }
        for (int i = 1; i <= n; i++)
        {
            rasp = max(rasp, (dr[i] - st[i] - 1) * a[i][j]);
        }
    }
    fcout << rasp;
    return 0;
}