Cod sursa(job #1727861)

Utilizator cristina_borzaCristina Borza cristina_borza Data 11 iulie 2016 19:40:26
Problema Teren Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <fstream>
#include <cstdio>

using namespace std;

int n , m , nr , x , sol , i , j , k , pos , st , ok;
int mat[305][305];

int main() {
    freopen("teren.in" , "r" , stdin);
    freopen("teren.out" , "w" , stdout);

    scanf("%d %d %d", &n, &m, &nr);
    for (i = 1; i <= n; ++i) {
        for (j = 1; j <= m; ++j) {
            scanf("%d", &x);
            mat[j][i] = mat[j][i - 1] + x;
        }
    }

    for (i = 1; i <= n; ++i) {
        for (j = i; j <= n; ++j) {
            pos = 1 , st = 0 , ok = 0;
            for (k = 1; k <= m; ++k) {
                st += mat[k][j] - mat[k][i - 1];
                while (st > nr && pos <= k) {
                    st -= (mat[pos][j] - mat[pos][i - 1]);
                    ++pos;
                }

                if (pos <= k) {
                    ok = 1;
                }

                if ((j - i + 1) * (k - pos + 1) > sol)
                    sol = (j - i + 1) * (k - pos + 1);
            }

            if (!ok) {
                break;
            }
        }
    }

    printf("%d" , sol);
    return 0;
}