We need to find a value that satisfies two conditions: it is the minimum in its row and the maximum in its column. If multiple such cells exist, the cost may be the same or different? The problem states: "He knows that sometimes the gift is not determined uniquely even if a gift exists whose cost is smallest in its row, but largest in its column. However, since the boy is so smart, he realizes that the gift's cost is determined uniquely. Your task is to tell him the gift's cost which is smallest in its row, but largest in its column, or to tell him no such gift exists." So even if multiple cells satisfy the property, but they have the same cost, the cost is uniquely determined. But if there are multiple cells with different costs that both satisfy the property? Could that happen? Possibly yes? Let's consider: If two cells have different costs both being row-min and column-max, but they are in different rows and columns. Could that happen? Possibly, but the problem statement says "He knows that sometimes the gift is not determined uniquely even if a gift exists whose cost is smallest in its row, but largest in its column. However, since the boy is so smart, he realizes that the gift's cost is determined uniquely." This suggests that even if there are multiple cells satisfying, the cost value might be the same across all such cells. The problem asks to output that unique cost or "GUESS" if no such cell exists. But careful: It says "the gift's cost is determined uniquely." That might be a property we can rely on: if there exists at least one cell that is a row-min and column-max, then all such cells have the same value. Is that always true? Let's think: Suppose we have two cells (r1,c1) and (r2,c2) each being the min in its row and max in its column. Let v1 = a[r1][c1], v2 = a[r2][c2]. Since v1 is min in row r1, and v2 is max in column c2. But we need to relate them. Could they be different? Consider: v1 <= all a[r1][c] for any c, specifically v1 <= a[r1][c2]. Also v2 >= all a[r][c2] for any r, specifically v2 >= a[r1][c2]. So v1 <= a[r1][c2] <= v2, thus v1 <= v2. Similarly, by symmetry: v2 <= all a[r2][c] for any c, so v2 <= a[r2][c1]; and v1 >= all a[r][c1] for any r, so v1 >= a[r2][c1]; then v2 <= a[r2][c1] <= v1, thus v2 <= v1. So v1 <= v2 and v2 <= v1, so v1 = v2. Therefore, indeed if there are multiple such cells, they must have the same value. So the cost is uniquely determined if any such cell exists. So we can find any cell that satisfies the condition and output its value; or if none, output "GUESS". That's straightforward.
But careful: The condition "smallest in its row" means it is a minimum element in its row. Since all costs positive, but there could be ties for minimum. The cell should be one of the minima? The condition: "gift whose cost is smallest in its row, but largest in its column." So if there are multiple minima in the row, the cell's value is equal to the minimum value of that row; that qualifies as "smallest in its row" because it is one of the smallest. Typically, "smallest in its row" means it's the minimum element of that row (maybe not unique). Example 1 shows they consider both 8s in row1 as minima; they consider the first 8 and check its column. So indeed, if the cell value equals the row minimum, it's acceptable.
Similarly, "largest in its column" means it is the maximum element of its column; ties also okay? The condition is "largest in its column" meaning it is the maximum in that column; if there are multiple maxima, it's okay because it's one of the largest. In the uniqueness proof, we used that v1 is >= all entries in its column, so if ties exist, v1 >= others, so equality allowed. So it's fine.
Thus algorithm: For each row, find the minimum value of that row (rowMin[r]). For each column, find the maximum value of that column (colMax[c]). Then iterate over all cells (r,c): if a[r][c] == rowMin[r] and a[r][c] == colMax[c], then output a[r][c] and return. If none found, output "GUESS". Complexity O(R*C). R,C up to 100, easy.
But wait: Could there be multiple such cells with different values? As proven, no, they must be equal. So first found is fine.
But note: In Example 2, no such cell, output GUESS.
In Example 3, all entries 10. For each row, rowMin=10, each column colMax=10. So all cells satisfy both? Check: cell (1,1): value 10 equals rowMin[1]=10 and colMax[1]=10. So condition holds. Output 10. Indeed unique cost.
Thus simple.
Edge Cases: R=1, C=1 => the only element is both min of row and max of col. So output that value.
R=1, C>1: rowMin is min of that row. For each column, colMax is max of column. Since only one row, colMax[c] = that entry. So we need a cell where a[0][c] equals rowMin (which is min across row) and also equals colMax[c] (which is itself). So condition reduces to: a[0][c] is the minimum in the row. Because colMax condition automatically true since it's the only element in that column. So if the minimum occurs at column c, that cell qualifies. So output that minimum. If there are multiple minima, they all qualify and have the same min value, so output min. So okay.
C=1, R>1: similar.
Thus implementation straightforward.
Now: The output is "GUESS" (all caps) if no such cell.
Implementation steps:
- Read R, C.
- Create matrix as list of lists of ints.
- Compute rowMin: list length R. For each row, compute min.
- Compute colMax: list length C. For each column, compute max. (Initialize to 0? But all positive, so fine. Or use first element).
- For r in range(R):
for c in range(C):
if matrix[r][c] == rowMin[r] and matrix[r][c] == colMax[c]:
print(matrix[r][c]); return
- print("GUESS")
That's it.
Now, what about reading input: first line: two ints separated by space; then R lines each with C ints.
We need to print integer without quotes.
Test with given examples.
Example 1:
R=2,C=3
matrix: [9,8,8], [2,6,11]
rowMin: row0 min=8, row1 min=2
colMax: col0 max(9,2)=9; col1 max(8,6)=8; col2 max(8,11)=11
Check cells:
(0,0):9 vs rowMin8->false; (0,1):8 vs 8 and colMax 8 -> true => output 8. Good.
Example 2:
Matrix:
9 8 11
2 6 34
5 9 11
rowMin: row0 min=8? Actually row0: 9,8,11 => min=8; row1:2,6,34 => min=2; row2:5,9,11=> min=5.
colMax: col0:9,2,5 => max=9; col1:8,6,9 => max=9; col2:11,34,11 => max=34.
Check cells:
(0,0):9 vs rowMin8 false
(0,1):8 vs rowMin8 true, vs colMax1=9 false.
(0,2):11 vs 8 false
(1,0):2 vs rowMin2 true, vs colMax0=9 false
(1,1):6 vs 2 false
(1,2):34 vs 2 false? Actually rowMin2, so 34 vs 2 false
(2,0):5 vs rowMin5 true, vs colMax0=9 false
(2,1):9 vs 5 false
(2,2):11 vs 5 false
None => GUESS.
Example 3:
matrix all 10.
rowMin all 10, colMax all 10.
Any cell works => output 10.
All good.
Now, constraints: values up to 1e8, positive.
Implementation in Python:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
R = int(next(it))
C = int(next(it))
matrix = []
for _ in range(R):
row = [int(next(it)) for _ in range(C)]
matrix.append(row)
rowMin = [min(row) for row in matrix]
colMax = [max(matrix[r][c] for r in range(R)) for c in range(C)]
for r in range(R):
for c in range(C):
if matrix[r][c] == rowMin[r] and matrix[r][c] == colMax[c]:
print(matrix[r][c])
return
print("GUESS")
That's it.
But wait: The problem statement says: "If no such gift exists, then print 'GUESS'". And also "Your task is to tell him the gift's cost which is smallest in its row, but largest in its column, or to tell him no such gift exists." So we output the integer if exists, else "GUESS". So correct.
Now double-check uniqueness proof: If there are two such cells with different values, we derived contradiction. So any found cell's value is the unique cost.
Thus answer done.
Now note: There is nuance: "smallest in its row, but largest in its column." Some might interpret that the cell must be the unique smallest in its row and unique largest in its column, but sample 1 includes ties: row1 minima are 8 and 8, they consider 8 as smallest (not necessarily unique). So ties allowed.
Thus algorithm is correct.
Now just implement.
Time complexity O(R*C).
We'll produce final answer.
But wait: Input may have trailing newline etc. Use sys.stdin.
Potential pitfalls: For large R,C (100), it's fine.
Edge: R=0? Not possible.
Now final code.
We'll produce with solve() and if name == 'main': solve().
All set. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
R = int(next(it))
C = int(next(it))
matrix = []
for _ in range(R):
row = [int(next(it)) for _ in range(C)]
matrix.append(row)
# minimum of each row
row_min = [min(row) for row in matrix]
# maximum of each column
col_max = [max(matrix[r][c] for r in range(R)) for c in range(C)]
for r in range(R):
for c in range(C):
val = matrix[r][c]
if val == row_min[r] and val == col_max[c]:
print(val)
return
print("GUESS")
if name == "main":
solve()