Approach
Sorting and greedy selection
For Codeforces 1777C — Quiz Master, 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
- 69 lines of Go from the credited upstream file 1777C.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 "bufio"5 . "fmt"6 "io"7 "slices"8)9 1011func cf1777C(_r io.Reader, _w io.Writer) {12 in := bufio.NewReader(_r)13 out := bufio.NewWriter(_w)14 defer out.Flush()15 const mx = 10000116 ds := [mx][]int{}17 for i := 1; i < mx; i++ {18 for j := i; j < mx; j += i {19 ds[j] = append(ds[j], i)20 }21 }22 23 var T, n, m int24 for Fscan(in, &T); T > 0; T-- {25 Fscan(in, &n, &m)26 a := make([]int, n)27 for i := range a {28 Fscan(in, &a[i])29 }30 slices.Sort(a)31 a = slices.Compact(a)32 33 ans := int(1e9)34 todo := m35 l := 036 cnt := make([]int, m+1) 37 for _, v := range a {38 for _, d := range ds[v] {39 if d > m {40 break41 }42 if cnt[d] == 0 {43 todo--44 }45 cnt[d]++46 }47 for todo == 0 {48 ans = min(ans, v-a[l])49 for _, d := range ds[a[l]] {50 if d > m {51 break52 }53 cnt[d]--54 if cnt[d] == 0 {55 todo++56 }57 }58 l++59 }60 }61 if ans == 1e9 {62 ans = -163 }64 Fprintln(out, ans)65 }66}67 6869