#C6979. Maximum Fertility Subplot
Maximum Fertility Subplot
Maximum Fertility Subplot
A farmer has a rectangular field divided into a grid with n rows and m columns. Each cell in the grid has a fertility value, represented by a positive integer. The farmer wants to find a rectangular subplot within the field that maximizes the sum of fertility values.
The fertility value of a plot with top-left corner (i, j) and bottom-right corner (k, l) can be computed using the formula:
\( S = \sum_{p=i}^{k} \sum_{q=j}^{l} a_{pq} \)
where \( a_{pq} \) is the fertility at cell (p, q) and the indices are 1-indexed. If multiple plots yield the same maximum fertility, any one of them can be returned.
Your task is to write a program that reads the dimensions and fertility grid from standard input, computes the rectangular subplot with the maximum fertility sum, and outputs the maximum sum along with the coordinates of its top-left and bottom-right corners.
inputFormat
The input is provided via standard input with the following format:
n m row1_element1 row1_element2 ... row1_elementm row2_element1 row2_element2 ... row2_elementm ... rown_element1 rown_element2 ... rown_elementm
Where the first line contains two integers, n and m (the number of rows and columns respectively). This is followed by n lines each containing m integers representing the fertility values of the field.
outputFormat
Output via standard output a single line containing five space-separated integers:
max_fertility top_left_row top_left_col bottom_right_row bottom_right_col
Here, max_fertility
is the maximum fertility sum found, and the remaining four numbers denote the coordinates of the top-left and bottom-right corners of the subplot, using 1-indexing.
3 3
1 2 3
4 5 6
7 8 9
45 1 1 3 3