- 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
- 60 lines of Go from the credited upstream file 1716C.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 CF1716C(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 min := func(a, b int) int {15 if a > b {16 return b17 }18 return a19 }20 max := func(a, b int) int {21 if b > a {22 return b23 }24 return a25 }26 27 var T, m int28 for Fscan(in, &T); T > 0; T-- {29 Fscan(in, &m)30 a := make([][2]int, m)31 for j := 0; j < 2; j++ {32 for i := 0; i < m; i++ {33 Fscan(in, &a[i][j])34 }35 }36 37 38 39 a[0][0] = -140 41 42 suf := make([][2]int, m+1)43 for i := m - 1; i >= 0; i-- {44 suf[i][0] = max(suf[i+1][0]-1, max(a[i][0], a[i][1]-(m-i)*2+1))45 suf[i][1] = max(suf[i+1][1]-1, max(a[i][1], a[i][0]-(m-i)*2+1))46 }47 48 49 ans, t := suf[0][0]+m*2, -150 for i, col := range a[:m-1] {51 t = max(t, col[i&1]) + 152 t = max(t, col[i&1^1]) + 153 ans = min(ans, max(t, suf[i+1][i&1^1])+(m-1-i)*2)54 }55 Fprintln(out, ans)56 }57}58 5960