Approach
Direct simulation
For Codeforces 1204C — Anna, Svyatoslav and Maps, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.
- 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
- 64 lines of Go from the credited upstream file 1204C.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 CF1204C(_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 a17 }18 return b19 }20 21 var n, m int22 var s []byte23 Fscan(in, &n)24 d := make([][]int, n)25 for i := range d {26 d[i] = make([]int, n)27 Fscan(in, &s)28 for j, b := range s {29 if b == '1' {30 d[i][j] = 131 } else if j != i {32 d[i][j] = 1e933 }34 }35 }36 for k := range d {37 for i := range d {38 for j := range d {39 d[i][j] = min(d[i][j], d[i][k]+d[k][j])40 }41 }42 }43 44 Fscan(in, &m)45 p := make([]int, m)46 for i := range p {47 Fscan(in, &p[i])48 }49 ans := []interface{}{p[0]}50 v := p[0] - 151 for i := 1; i < m-1; i++ {52 w, u := p[i]-1, p[i+1]-153 if d[v][w]+d[w][u] > d[v][u] {54 ans = append(ans, w+1)55 v = w56 }57 }58 ans = append(ans, p[m-1])59 Fprintln(out, len(ans))60 Fprint(out, ans...)61}62 6364