Cod sursa(job #3133434)

Utilizator z.catincaCatinca Zavoianu z.catinca Data 25 mai 2023 17:18:30
Problema Plantatie Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main() {
    ifstream f("plantatie.in");
    int N, M;
    f >> N >> M;

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

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

    ofstream g("plantatie.out");
    for (int i = 0; i < M; i++) {
        int query_i, query_j, query_k;
        f >> query_i >> query_j >> query_k;

        int max_productivity = 0;
        for (int row = query_i - 1; row < query_i - 1 + query_k; row++) {
            for (int col = query_j - 1; col < query_j - 1 + query_k; col++) {
                max_productivity = max(max_productivity, productivity[row][col]);
            }
        }

        g << max_productivity << endl;
    }

    f.close();
    g.close();

    return 0;
}