- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 57 lines of Go from the credited upstream file 489E.go.
- The implementation visibly relies on sequence storage, cached states.
- No explicit loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
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 "math"8)9 1011func CF489E(_r io.Reader, _w io.Writer) {12 in := bufio.NewReader(_r)13 out := bufio.NewWriter(_w)14 defer out.Flush()15 16 var n int17 var lim float6418 Fscan(in, &n, &lim)19 x := make([]float64, n+1)20 b := make([]float64, n+1)21 for i := 1; i <= n; i++ {22 Fscan(in, &x[i], &b[i])23 }24 25 dp := make([]float64, n+1)26 from := make([]int, n+1)27 l, r := 0., 1e328 for step := 40; step > 0; step-- {29 mid := (l + r) / 230 for i := 1; i <= n; i++ {31 dp[i] = 1e9932 for j, dv := range dp[:i] {33 res := dv + math.Sqrt(math.Abs(x[i]-x[j]-lim)) - b[i]*mid34 if res < dp[i] {35 dp[i] = res36 from[i] = j37 }38 }39 }40 if dp[n] <= 0 {41 r = mid42 } else {43 l = mid44 }45 }46 47 ans := []int{}48 for i := n; i > 0; i = from[i] {49 ans = append(ans, i)50 }51 for i := len(ans) - 1; i >= 0; i-- {52 Fprint(out, ans[i], " ")53 }54}55 5657