Cod sursa(job #3134050)

Utilizator vatau.lorenaVatau Lorena vatau.lorena Data 28 mai 2023 00:23:29
Problema Plantatie Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

const string inputFilename = "plantatie.in";
const string outputFilename = "plantatie.out";

int main() {
    ifstream fin(inputFilename);
    ofstream fout(outputFilename);

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

    vector<vector<int>> plantation(N, vector<int>(N));

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

    vector<vector<int>> maxInRow(N, vector<int>(N, 0));
    vector<vector<int>> maxInCol(N, vector<int>(N, 0));

    // Precompute maximum values in each row and column
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            maxInRow[i][j] = (j == 0) ? plantation[i][j] : max(plantation[i][j], maxInRow[i][j - 1]);
            maxInCol[j][i] = (i == 0) ? plantation[i][j] : max(plantation[i][j], maxInCol[j][i - 1]);
        }
    }

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

        int maxVal = 0;

        // Check for each subgrid of size k x k
        for (int a = i - 1; a < i + k - 1; ++a) {
            if (a >= N) break;
            for (int b = j - 1; b < j + k - 1; ++b) {
                if (b >= N) break;

                int rowMax = maxInRow[a][b];
                int colMax = maxInCol[b][a];

                maxVal = max(maxVal, max(rowMax, colMax));
            }
        }

        fout << maxVal << "\n";
    }

    fin.close();
    fout.close();

    return 0;
}