Approach
Direct simulation
For Codeforces 1528D — It's a bird! No, it's a plane! No, it's AaParsa!, 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
- 65 lines of Go from the credited upstream file 1528D.go.
- The implementation visibly relies on sequence storage.
- 1 loop block 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 CF1528D(_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 21 var n, m, v, w, wt int22 Fscan(in, &n, &m)23 g := make([][]int, n)24 for i := range g {25 g[i] = make([]int, n)26 for j := range g[i] {27 g[i][j] = 1e9 + 1e328 }29 }30 for ; m > 0; m-- {31 Fscan(in, &v, &w, &wt)32 g[v][w] = wt33 }34 for st, row := range g {35 dis := append([]int{}, row...)36 vis := make([]bool, n)37 for {38 v := -139 for w, b := range vis {40 if !b && (v < 0 || dis[w] < dis[v]) {41 v = w42 }43 }44 if v < 0 {45 break46 }47 vis[v] = true48 d := dis[v]49 dis[(v+1)%n] = min(dis[(v+1)%n], d+1)50 for j, wt := range g[v] {51 dis[(j+d)%n] = min(dis[(j+d)%n], d+wt)52 }53 }54 for i, d := range dis {55 if i == st {56 d = 057 }58 Fprint(out, d, " ")59 }60 Fprintln(out)61 }62}63 6465