- 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
- 70 lines of Go from the credited upstream file 1181C.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 CF1181C(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 min := func(a, b int) int {13 if a > b {14 return b15 }16 return a17 }18 19 var n, m, ans int20 Fscan(in, &n, &m)21 a := make([]string, n)22 for i := range a {23 Fscan(in, &a[i])24 }25 26 f := make([]int, n)27 for j := 0; j < m; j++ {28 for i, row := range a {29 if j > 0 && row[j] == row[j-1] {30 f[i]++31 } else {32 f[i] = 133 }34 }35 next:36 for i := 0; i < n; {37 i0 := i38 mn := f[i] 39 for i++; i < n && a[i][j] == a[i0][j]; i++ {40 mn = min(mn, f[i])41 }42 43 size := i - i044 if i0 < size || i+size > n {45 continue46 }47 48 49 for k := i0 - 1; k >= i0-size; k-- {50 if a[k][j] != a[i0-1][j] {51 continue next52 }53 mn = min(mn, f[k])54 }55 56 57 for k := i; k < i+size; k++ {58 if a[k][j] != a[i][j] {59 continue next60 }61 mn = min(mn, f[k])62 }63 ans += mn64 }65 }66 Fprint(out, ans)67}68 6970