- 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
- 76 lines of Go from the credited upstream file 1261C.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 "sort"8)9 1011func CF1261C(_r io.Reader, _w io.Writer) {12 in := bufio.NewReader(_r)13 out := bufio.NewWriter(_w)14 defer out.Flush()15 min := func(a, b int) int {16 if a < b {17 return a18 }19 return b20 }21 max := func(a, b int) int {22 if a > b {23 return a24 }25 return b26 }27 28 var n, m int29 Fscan(in, &n, &m)30 var s string31 a := make([][]int, n+1)32 b := make([][]int, n+2)33 a[0] = make([]int, m+1)34 b[n+1] = make([]int, m+2)35 for i := 0; i < n; i++ {36 a[i+1] = make([]int, m+1)37 b[i+1] = make([]int, m+2)38 Fscan(in, &s)39 for j, v := range s {40 if v == 'X' {41 a[i+1][j+1] = min(min(a[i][j], a[i][j+1]), a[i+1][j]) + 142 }43 }44 }45 46 t := sort.Search(min(n, m), func(t int) bool {47 t++48 for i := n; i > 0; i-- {49 for j := m; j > 0; j-- {50 if a[i][j] > t*2 {51 b[i][j] = a[i][j]52 } else {53 b[i][j] = max(1, max(max(b[i+1][j], b[i+1][j+1]), b[i][j+1])) - 154 if b[i][j] == 0 && a[i][j] > 0 {55 return true56 }57 }58 }59 }60 return false61 })62 Fprintln(out, t)63 for i := 1; i <= n; i++ {64 for j := 1; j <= m; j++ {65 if i+t <= n && j+t <= m && a[i+t][j+t] > t*2 {66 Fprint(out, "X")67 } else {68 Fprint(out, ".")69 }70 }71 Fprintln(out)72 }73}74 7576