1684C.md (2073B)
1 Source: https://codeforces.com/problemset/problem/1684/C 2 3 C. Column Swapping 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given a grid with $n$ rows and $m$ columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each $1 \le i \le n$ and $2 \le j \le m$ the following holds: $a_{i,j} \ge a_{i, j-1}$. 22 23 You have to to do the following operation exactly once: choose two columns with indexes $i$ and $j$ (not necessarily different), $1 \le i, j \le m$, and swap them. 24 25 You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped. 26 27 Input 28 29 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. 30 31 The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of rows and columns respectively. 32 33 Each of the next $n$ rows contains $m$ integers, $j$-th element of $i$-th row is $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$) — the number written in the $j$-th cell of the $i$-th row. 34 35 It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$. 36 37 Output 38 39 If after the swap it is impossible to get a good grid, output $-1$. 40 41 In the other case output $2$ integers — the indices of the columns that should be swapped to get a good grid. 42 43 If there are multiple solutions, print any. 44 45 Example 46 47 Input 48 49 ``` 50 5 51 52 2 3 53 54 1 2 3 55 56 1 1 1 57 58 2 2 59 60 4 1 61 62 2 3 63 64 2 2 65 66 2 1 67 68 1 1 69 70 2 3 71 72 6 2 1 73 74 5 4 3 75 76 2 1 77 78 1 79 80 2 81 ``` 82 83 Output 84 85 ``` 86 1 1 87 -1 88 1 2 89 1 3 90 1 1 91 ``` 92 93 Note 94 95 In the first test case the grid is initially good, so we can, for example, swap the first column with itself. 96 97 In the second test case it is impossible to make the grid good. 98 99 In the third test case it is needed to swap the first and the second column, then the grid becomes good.