Approach
Sorting and greedy selection
For Codeforces 1980E — Permutation of Rows and Columns, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 58 lines of Go from the credited upstream file 1980E.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1package main2 3import (4 . "fmt"5 "io"6 "slices"7)8 910func cf1980E(in io.Reader, out io.Writer) {11 var T, n, m int12o:13 for Fscan(in, &T); T > 0; T-- {14 Fscan(in, &n, &m)15 f := func() (rows, cols [][]int) {16 rows = make([][]int, n)17 cols = make([][]int, m)18 for i := range cols {19 cols[i] = make([]int, n)20 }21 for i := range rows {22 rows[i] = make([]int, m)23 for j := range rows[i] {24 Fscan(in, &rows[i][j])25 cols[j][i] = rows[i][j]26 }27 slices.Sort(rows[i])28 }29 for _, col := range cols {30 slices.Sort(col)31 }32 cmp := func(a, b []int) int { return slices.Compare(a, b) }33 slices.SortFunc(rows, cmp)34 slices.SortFunc(cols, cmp)35 return36 }37 38 rows1, cols1 := f()39 rows2, cols2 := f()40 41 for i, r1 := range rows1 {42 if !slices.Equal(r1, rows2[i]) {43 Fprintln(out, "NO")44 continue o45 }46 }47 for i, c1 := range cols1 {48 if !slices.Equal(c1, cols2[i]) {49 Fprintln(out, "NO")50 continue o51 }52 }53 Fprintln(out, "YES")54 }55}56 5758