Cod sursa(job #1727860)

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

using namespace std;

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

void read(int &x) {
    char ch;
    x = 0;

    while (!isdigit(ch = getchar())) {

    }

    do {
        x = x * 10 + ch - '0';
    } while (isdigit(ch = getchar()));
}

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

    read(n) , read(m) , read(nr);
    for (i = 1; i <= n; ++i) {
        for (j = 1; j <= m; ++j) {
            read(x);
            mat[j][i] = x + mat[j][i - 1];
        }
    }

    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;
                }

                sol = max(sol , (j - i + 1) * (k - pos + 1));
            }

            if (!ok) {
                break;
            }
        }
    }

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