- 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
- 79 lines of Go from the credited upstream file 888G.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"8)9 1011type node88 struct{ son [2]*node88 }12type trie88 struct{ root *node88 }13 14const trieBitLen88 = 3015 16func (t *trie88) put(v int32) *node88 {17 o := t.root18 for i := trieBitLen88 - 1; i >= 0; i-- {19 b := v >> i & 120 if o.son[b] == nil {21 o.son[b] = &node88{}22 }23 o = o.son[b]24 }25 return o26}27 28func (t *trie88) minXor(v int32) (ans int) {29 o := t.root30 for i := trieBitLen88 - 1; i >= 0; i-- {31 b := v >> i & 132 if o.son[b] == nil {33 ans |= 1 << i34 b ^= 135 }36 o = o.son[b]37 }38 return39}40 41func CF888G(_r io.Reader, out io.Writer) {42 in := bufio.NewReader(_r)43 var n, ans int44 Fscan(in, &n)45 a := make([]int32, n)46 for i := range a {47 Fscan(in, &a[i])48 }49 50 var f func([]int32, int)51 f = func(a []int32, p int) {52 if a == nil || p < 0 {53 return54 }55 b := [2][]int32{}56 for _, v := range a {57 k := v >> p & 158 b[k] = append(b[k], v)59 }60 if b[0] != nil && b[1] != nil {61 t := &trie88{&node88{}}62 for _, v := range b[0] {63 t.put(v)64 }65 res := math.MaxInt66 for _, v := range b[1] {67 res = min(res, t.minXor(v))68 }69 ans += res70 }71 f(b[0], p-1)72 f(b[1], p-1)73 }74 f(a, 29)75 Fprint(out, ans)76}77 7879