Cod sursa(job #2801170)

Utilizator lolismekAlex Jerpelea lolismek Data 15 noiembrie 2021 11:55:28
Problema Teren Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.27 kb
/**
#include <iostream>
#include <fstream>
#include <queue>

using namespace std;

ofstream fout("teren.out");

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

const int N = 300;
int teren[N + 2][N + 2], sum[N + 2];

void reset(int m){
    int i;
    for(i = 1; i <= m; i++) sum[i] = 0;
}

int main()
{
    InParser fin("teren.in");
    int n, m, x, i1, i2, j, amax, s, elim, l;
    fin >> n >> m >> x;
    for(i1 = 1; i1 <= n; i1++)
        for(j = 1; j <= m; j++) fin >> teren[i1][j];
    amax = 0;
    for(i1 = 1; i1 <= n; i1++){
        reset(m);
        for(i2 = i1; i2 <= n; i2++){
            /// cautam subsirul de lungime maxima cu suma cel mult x in vectorul sum
            queue <int> Q;
            s = 0;
            for(j = 1; j <= m; j++){
                sum[j] += teren[i2][j];
                Q.push(sum[j]);
                s += sum[j];
                while(!Q.empty() && s > x){
                    elim = Q.front();
                    s -= elim;
                    Q.pop();
                }
                l = Q.size();
                amax = max(amax, l * (i2 - i1 + 1));
            }
        }
    }
    fout << amax;
    return 0;
}
ayaye*/
#include <fstream>
using namespace std;
int sp[305][305];
ifstream cin("teren.in");
ofstream cout("teren.out");
int main(){
    int n, m, x, nr;
    cin >> n >> m >> x;
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= m; j ++){
            cin >> nr;
            sp[i][j] = sp[i - 1][j] + nr;
        }
    }
    int maxx = -1;
    for(int l1 = 1; l1 <= n; l1 ++){
        for(int l2 = l1; l2 <= n; l2 ++){
            int st = 1, sum = 0;
            for(int k = 1; k <= m; k ++){
                sum = sum + sp[l2][k] - sp[l1 - 1][k];
                while(st <= k && sum > x){
                    sum = sum - (sp[l2][st] - sp[l1 - 1][st]);
                    st ++;
                }
                if(maxx < (l2 - l1 + 1) * (k - st + 1)){
                    maxx = (l2 - l1 + 1) * (k - st + 1);
                }
            }
        }
    }
    cout << maxx;
    return 0;
}