- 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
- 64 lines of Go from the credited upstream file 1601B.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)8 910func CF1601B(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 15 var n int16 Fscan(in, &n)17 dec := make([]int, n)18 for i := range dec {19 Fscan(in, &dec[i])20 }21 inc := make([]int, n)22 for i := range inc {23 Fscan(in, &inc[i])24 }25 26 vis := make([]bool, n)27 vis[n-1] = true28 type pair struct{ h, fa int }29 from := make([]pair, n)30 q := []int{n - 1}31 for step, min := 1, n-1; q != nil; step++ {32 tmp := q33 q = nil34 for _, v := range tmp {35 st := v - dec[v]36 if st < 0 {37 Fprintln(out, step)38 path := make([]int, 1, step)39 for ; v != n-1; v = from[v].fa {40 path = append(path, from[v].h+1)41 }42 for i := len(path) - 1; i >= 0; i-- {43 Fprint(out, path[i], " ")44 }45 return46 }47 if st >= min {48 continue49 }50 for h := st; h < min; h++ {51 if w := h + inc[h]; !vis[w] {52 vis[w] = true53 from[w] = pair{h, v}54 q = append(q, w)55 }56 }57 min = st58 }59 }60 Fprint(out, -1)61}62 6364