- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 87 lines of Go from the credited upstream file 1886E.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
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 "math/bits"8 "slices"9)10 1112func cf1886E(in io.Reader, _w io.Writer) {13 out := bufio.NewWriter(_w)14 defer out.Flush()15 var n, m, b int16 Fscan(in, &n, &m)17 type pair struct{ v, i int }18 a := make([]pair, n)19 for i := range a {20 Fscan(in, &a[i].v)21 a[i].i = i22 }23 slices.SortFunc(a, func(a, b pair) int { return b.v - a.v })24 25 minR := make([][]int, m)26 for i := range minR {27 Fscan(in, &b)28 mn := make([]int, n)29 for j, p := range a {30 mn[j] = 1e931 need := (b-1)/p.v + 132 if l := j - need + 1; l >= 0 {33 mn[l] = min(mn[l], j)34 }35 }36 for j := n - 2; j >= 0; j-- {37 mn[j] = min(mn[j], mn[j+1])38 }39 minR[i] = mn40 }41 42 u := 1 << m43 f := make([]int, u)44 for i := range f {45 f[i] = 1e946 }47 f[0] = 048 from := make([]int, u)49 for s, man := range f {50 if man >= n {51 continue52 }53 for t, lb := u-1^s, 0; t > 0; t ^= lb {54 lb = t & -t55 ns := s | lb56 i := bits.TrailingZeros32(uint32(lb))57 need := minR[i][man] - man + 158 if man+need < f[ns] {59 f[ns] = man + need60 from[ns] = s61 }62 }63 }64 if f[u-1] > n {65 Fprint(out, "NO")66 return67 }68 69 type pair2 struct{ l, r int }70 ans := make([]pair2, m)71 for i := u - 1; i > 0; i = from[i] {72 j := from[i]73 ans[bits.TrailingZeros32(uint32(i^j))] = pair2{f[j], f[i]}74 }75 76 Fprintln(out, "YES")77 for _, p := range ans {78 Fprint(out, p.r-p.l)79 for _, q := range a[p.l:p.r] {80 Fprint(out, " ", q.i+1)81 }82 Fprintln(out)83 }84}85 8687