- 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
- 66 lines of Go from the credited upstream file 1292B.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 . "fmt"5 "io"6)7 89func CF1292B(_r io.Reader, _w io.Writer) {10 type point struct{ x, y int64 }11 abs := func(x int64) int64 {12 if x < 0 {13 return -x14 }15 return x16 }17 dis := func(a, b point) int64 { return abs(a.x-b.x) + abs(a.y-b.y) }18 19 var ax, ay, bx, by, tt int6420 var p, st point21 Fscan(_r, &p.x, &p.y, &ax, &ay, &bx, &by, &st.x, &st.y, &tt)22 ps := []point{}23 24 for ; p.x < 9e16 && p.y < 9e16; p.x, p.y = ax*p.x+bx, ay*p.y+by {25 ps = append(ps, p)26 }27 28 ans := 029 for i, p := range ps {30 t := tt31 32 if d := dis(st, p); d <= t {33 t -= d34 c := 135 for j := i; j > 0; j-- {36 if d := dis(ps[j], ps[j-1]); d <= t {37 t -= d38 c++39 } else {40 break41 }42 }43 if i+1 < len(ps) {44 if d := dis(ps[0], ps[i+1]); d <= t {45 t -= d46 c++47 for j := i + 2; j < len(ps); j++ {48 if d := dis(ps[j], ps[j-1]); d <= t {49 t -= d50 c++51 } else {52 break53 }54 }55 }56 }57 if c > ans {58 ans = c59 }60 }61 }62 Fprint(_w, ans)63}64 6566