Approach
Sorting and greedy selection
For Codeforces 1835B — Lottery, 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
- 59 lines of Go from the credited upstream file 1835B.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 cf1835B(in io.Reader, out io.Writer) {11 var n, m, k int12 Fscan(in, &n, &m, &k)13 a := make([]int, n)14 for i := range a {15 Fscan(in, &a[i])16 }17 slices.Sort(a)18 19 p := []int{}20 add := func(x int) {21 for i := x - 2; i <= x+2; i++ {22 if 0 <= i && i <= m && (len(p) == 0 || i > p[len(p)-1]) {23 p = append(p, i)24 }25 }26 }27 28 add(0)29 for _, x := range a {30 add(x)31 }32 add(m)33 34 mx, ans := -1, 035 i, j := 0, 036 for _, x := range p {37 for i < n && x > a[i] {38 i++39 }40 for j < n && x >= a[j] {41 j++42 }43 l, r := 0, m44 if j >= k {45 l = (a[j-k]+x)/2 + 146 }47 if i+k <= n {48 r = (a[i+k-1] + x - 1) / 249 }50 if r-l+1 > mx {51 mx = r - l + 152 ans = x53 }54 }55 Fprint(out, mx, ans)56}57 5859