#C9274. Forest Analysis
Forest Analysis
Forest Analysis
You are given a forest represented as a grid of r rows and c columns. Each cell contains an integer. A value of 0 means the cell is empty, and a positive integer represents a tree of a particular species. Your task is to analyze the grid and determine:
- The total number of distinct tree species (ignoring cells with 0).
- The species identifier which has the maximum number of trees. In case of a tie, choose the smallest identifier.
If the grid does not contain any trees, output 0 0.
The mathematical formulation can be described as follows: Let \( S \) be the set of species such that for each \( s \in S \), the frequency \( f(s) > 0 \). Then, output \(|S|\) and \(\min\{s : f(s) = \max_{t \in S} f(t)\}\). All formulas are written in \(\LaTeX\) format.
inputFormat
The input is given via stdin and has the following format:
r c row1_cell1 row1_cell2 ... row1_cellc row2_cell1 row2_cell2 ... row2_cellc ... rowr_cell1 rowr_cell2 ... rowr_cellc
Here, r and c are integers that represent the number of rows and columns, respectively. Each of the following r lines contains c integers, separated by spaces.
outputFormat
Print two space-separated integers to stdout. The first integer is the number of distinct tree species (ignoring zeros) and the second integer is the species identifier with the maximum count. If there is a tie, output the smallest species identifier. If there are no trees, output 0 0.
## sample3 4
1 0 2 0
3 1 2 0
0 3 0 0
3 1