Column Swapping (Codeforces 1684C)
Tags: Greedy, sortings

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}$$$.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.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.

Input: 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.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.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.It's guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: If after the swap it is impossible to get a good grid, output $$$-1$$$.In the other case output $$$2$$$ integers — the indices of the columns that should be swapped to get a good grid.If there are multiple solutions, print any.

Note: In the first test case the grid is initially good, so we can, for example, swap the first column with itself.In the second test case it is impossible to make the grid good.In the third test case it is needed to swap the first and the second column, then the grid becomes good.

Sample Inputs:
5
2 3
1 2 3
1 1 1
2 2
4 1
2 3
2 2
2 1
1 1
2 3
6 2 1
5 4 3
2 1
1
2

Sample Outputs:
1 1
-1
1 2
1 3
1 1

Solution (Python):
from sys import stdin, stdout
for _ in range(int(stdin.readline())):
    (n, m) = map(int, stdin.readline().split())
    g = []
    ind1 = -2
    ind2 = -2
    f = True
    for i in range(n):
        a = list(map(int, stdin.readline().split()))
        g += [a]
        b = sorted(a)
        c = []
        if a == b:
            continue
        for j in range(m):
            if a[j] != b[j]:
                c += [j]
        if len(c) > 2:
            f = False
            continue
        if ind1 == -2:
            ind1 = c[0]
            ind2 = c[1]
            continue
        if ind1 != c[0] or ind2 != c[1]:
            f = False
            continue
    if f:
        if ind1 == -2:
            stdout.write('1 1\n')
        else:
            for i in range(n):
                b = sorted(g[i])
                (g[i][ind1], g[i][ind2]) = (g[i][ind2], g[i][ind1])
                if b != g[i]:
                    f = False
                    break
            if f:
                stdout.write(str(ind1 + 1) + ' ' + str(ind2 + 1) + '\n')
            else:
                stdout.write('-1\n')
    else:
        stdout.write('-1\n')

Solution (Python):
import sys
input = sys.stdin.readline
from collections import Counter

def s():
    (alr, p) = (False, [])
    (n, m) = map(int, input().split())
    for e in range(n):
        a = [int(d) for d in input().split()]
        if not alr:
            f = []
            s = sorted(a)
            for j in range(m):
                if a[j] != s[j]:
                    alr = True
                    f += [j]
        p += [a]
    if not alr:
        return '1 1'
    if len(f) > 2:
        return '-1'
    (x, y) = (f[0], f[1])
    for k in p:
        (k[x], k[y]) = (k[y], k[x])
        v = sorted(k)
        if k != v:
            return '-1'
    return str(x + 1) + ' ' + str(y + 1)
t = int(input())
res = ''
for z in range(t):
    res += s() + '\n'
print(res)

Solution (Python):
import sys
input = sys.stdin.readline

def solve():
    (N, M) = list(map(int, input().split()))
    X = []
    for i in range(M):
        X.append([])
    for i in range(N):
        K = list(map(int, input().split()))
        for j in range(M):
            X[j].append(K[j])
    flag = 0
    ans = set()
    Y = X.copy()
    for i in range(N):
        if flag:
            break
        for j in range(1, M):
            if X[j - 1][i] > X[j][i]:
                X.sort(key=lambda x: x[i])
                for l in range(M):
                    if X[l][i] != Y[l][i]:
                        ans.add(l + 1)
                continue
    for i in range(N):
        for j in range(1, M):
            if X[j - 1][i] > X[j][i]:
                print(-1)
                return
    if len(ans) == 0:
        print(1, 1)
    elif len(ans) >= 3:
        print(-1)
    else:
        print(*ans)
for _ in range(int(input())):
    solve()