Approach
Sorting and greedy selection
For Codeforces 1268D — Invertation in Tournament, 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
- 73 lines of Go from the credited upstream file 1268D.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 cf1268D(in io.Reader, out io.Writer) {11 var n int12 Fscan(in, &n)13 a := make([][]int, n+1)14 s := make([]string, n+1)15 for i := 1; i <= n; i++ {16 Fscan(in, &s[i])17 a[i] = make([]int, n+1)18 }19 20 c := make([]int, n+1)21 for i := 1; i <= n; i++ {22 for j := 1; j <= n; j++ {23 a[i][j] = int(s[i][j-1] - '0')24 c[i] += a[i][j]25 }26 }27 28 p := make([]int, n+1)29 check := func() bool {30 copy(p[1:], c[1:])31 slices.Sort(p[1:])32 t := 033 for i := 1; i < n; i++ {34 t += p[i]35 if t == i*(i-1)/2 {36 return false37 }38 }39 return true40 }41 42 swap := func(x int) {43 for i := 1; i <= n; i++ {44 c[x] -= a[x][i]45 c[i] -= a[i][x]46 a[x][i], a[i][x] = a[i][x], a[x][i]47 c[x] += a[x][i]48 c[i] += a[i][x]49 }50 }51 52 ans := 053 for i := 1; i <= n; i++ {54 swap(i)55 if check() {56 ans++57 }58 swap(i)59 }60 61 if check() {62 Fprint(out, 0, 1)63 } else if ans > 0 {64 Fprint(out, 1, ans)65 } else if n == 6 {66 Fprint(out, 2, 18)67 } else {68 Fprint(out, -1)69 }70}71 7273