- 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
- 67 lines of Go from the credited upstream file 1004D.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 CF1004D(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 abs := func(x int) int {13 if x < 0 {14 return -x15 }16 return x17 }18 19 var t, v, mx int20 Fscan(in, &t)21 cnt := make([]int, t)22 for range cnt {23 Fscan(in, &v)24 cnt[v]++25 if v > mx {26 mx = v27 }28 }29 row := 030 for ; row+1 < t && row*4+4 == cnt[row+1]; row++ {31 }32 f := func(n, m int) bool {33 col := n - 1 - row + m - 1 - mx34 if row >= n || col < 0 || col >= m {35 return false36 }37 c2 := make([]int, t)38 for i := 0; i < n; i++ {39 for j := 0; j < m; j++ {40 v := abs(i-row) + abs(j-col)41 if v >= t {42 return false43 }44 if c2[v]++; c2[v] > cnt[v] {45 return false46 }47 }48 }49 for i, c := range c2 {50 if c != cnt[i] {51 return false52 }53 }54 Fprintln(out, n, m)55 Fprintln(out, row+1, col+1)56 return true57 }58 for d := 1; d*d <= t; d++ {59 if t%d == 0 && (f(d, t/d) || f(t/d, d)) {60 return61 }62 }63 Fprint(out, -1)64}65 6667