当前位置:网站首页>Cf:c. column swapping [sort + simulate]

Cf:c. column swapping [sort + simulate]

2022-07-07 05:56:00 White speed Dragon King's review

 Insert picture description here

analysis

Record the position of each row that does not meet the sorting
If there are more than three direct coolings
And then use one set Record what needs to be changed (l,r)
If it is greater than 1 Cool
Then judge each row l r Must satisfy a[l] < a[r]
Otherwise it will be cool

ac code

import sys
input = sys.stdin.readline

for _ in range(int(input())):
    n, m = list(map(int, input().split()))
    grid = [[0] * m for _ in range(n)]
    for i in range(n):
        grid[i] = list(map(int, input().split()))
    flag = True
    mustChanges = set()
    for i in range(n):
        lst = []
        temp = sorted(grid[i])
        for j in range(m):
            if temp[j] != grid[i][j]:
                lst.append(j)
        if len(lst) > 2:
            flag = False
            break
        elif len(lst) == 2:
            mustChanges.add((lst[0], lst[1]))
        if len(mustChanges) >= 2:
            flag = False
            break




    if not flag:
        print(-1)
    else:
        if len(mustChanges) == 0:
            print(1, 1)
        else:
            l, r = list(mustChanges)[0]
            flag = True
            for i in range(n):
                if grid[i][l] < grid[i][r]:
                    flag = False
                    break
            if flag:
                print(l + 1, r + 1)
            else:
                print(-1)

summary

Sort + simulation

原网站

版权声明
本文为[White speed Dragon King's review]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070037372731.html