PROBLEM LINKS
Author: Constantine Sokol
Tester: Roman Rubanenko
Editorialist: Praveen Reddy Vaka
DIFFICULTY:
cakewalk
PREREQUISITES:
None
EXPLANATION
This is a straightforward implementation problem.
You have to read the first line of the test data to get the values N and M.
Then read the next N lines (M integers per line corresponding to a row in the matrix) to read the matrix.
Store the matrix in a 2D array A.
Read the next line to get the value of L.
Maintain two variables E1 and E2 initialized to 0.
While reading the next L lines of test data and perform the following tasks
Let the line read be “i j”
if E1 != -1
if A[i][j] doesn’t go out of bound set E1 = E1 + A[i][j]
else set E1 = -1
if E2 != -1
if A[j][i] doesn’t go out of bound set E2 = E2 + A[j][i]
else set E2 = -1
Report max(E1, E2)
To check if A[i][j] goes out of bound or not you just have to simply check if 0<=i<N and 0<=j<M. In languages like Java you can simply have your statement E1 = E1 + A[i][j] inside a try catch block and if it throws ArrayOutOfBoundsException then you set E1 to -1.
Comments on Sub Tasks:
Though the problem asks for a simple implementation there are chances you might not be able to get full score on all the subtasks if you have not paid close look to the constraints.
Each entry in the matrix can be as big as 10^9 though this fits in a 32 bit integer, if you use 32 bit integers to store your E1 and E2 variables you will pass only subtask1 and subtask2 going by the constraints. But you will not be passing subtask3, subtask4 and subtask5 due to overflow in the values. If you use 64-bit integers then you will pass all the subtasks.
SOLUTIONS
Setter’s Solution: MIKE1.cpp
Tester’s Solution: MIKE1.cpp
Editorialist’s Solution: MIKE1.java