#K37477. Row Sorting Operations
Row Sorting Operations
Row Sorting Operations
You are given a grid of dimensions \(n \times m\) consisting of integers. Your task is to determine the minimum number of operations required to ensure that every row in the grid is sorted in non-decreasing order. In one operation, you can sort an entire row if the row is not already sorted. However, note that if at least one row is unsorted, a single operation is performed on the grid as a whole.
More formally, if every row of the grid is already non-decreasing (i.e. for each row, \(a_1 \leq a_2 \leq \cdots \leq a_m\)), then the answer is 0. Otherwise, the answer is 1.
The grid is provided as \(n\) rows each containing \(m\) integers. You should read from standard input and print the result to standard output.
inputFormat
The input is given from standard input. The first line contains two space-separated integers \(n\) and \(m\) representing the number of rows and columns of the grid.
This is followed by \(n\) lines, each containing \(m\) space-separated integers representing the elements of each row of the grid.
outputFormat
Output a single integer: 0 if every row in the grid is already non-decreasing; otherwise, output 1.
## sample3 3
3 1 2
8 5 7
6 4 9
1