Cod sursa(job #3232122)

Utilizator maciucateodorMaciuca Teodor maciucateodor Data 28 mai 2024 23:56:49
Problema Plantatie Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <climits>
using namespace std;

int main() {
    ifstream fin("plantatie.in");
    ofstream fout("plantatie.out");

    int N, M;
    fin >> N >> M;

    vector<vector<int>> productivity(N, vector<int>(N));
    vector<vector<int>> prefix_max(N+1, vector<int>(N+1, INT_MIN));

    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            fin >> productivity[i][j];
        }
    }

    for (int i = 1; i <= N; ++i) {
        for (int j = 1; j <= N; ++j) {
            prefix_max[i][j] = productivity[i-1][j-1];
            if (i > 1) prefix_max[i][j] = max(prefix_max[i][j], prefix_max[i-1][j]);
            if (j > 1) prefix_max[i][j] = max(prefix_max[i][j], prefix_max[i][j-1]);
            if (i > 1 && j > 1) prefix_max[i][j] = max(prefix_max[i][j], prefix_max[i-1][j-1]);
        }
    }

    for (int q = 0; q < M; ++q) {
        int i, j, k;
        fin >> i >> j >> k;

        int max_prod = INT_MIN;
        for (int x = i; x < i + k; ++x) {
            for (int y = j; y < j + k; ++y) {
                max_prod = max(max_prod, productivity[x-1][y-1]);
            }
        }
        fout << max_prod << '\n';
    }

    return 0;
}