- 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
- 103 lines of Go from the credited upstream file 6D.go.
- The implementation visibly relies on sequence storage, cached states.
- 2 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 . "fmt"5 "io"6)7 89func cf6D(in io.Reader, out io.Writer) {10 var n, fi, se int11 Fscan(in, &n, &fi, &se)12 a := make([]int, n)13 for i := range a {14 Fscan(in, &a[i])15 a[i]++16 }17 18 k := (a[0]-1)/se + 119 type pair struct{ i, c int }20 ans := []pair{{1, k}}21 a[1] -= k * fi22 a[2] -= k * se23 24 if a[n-1] > 0 {25 k = (a[n-1]-1)/se + 126 ans = append(ans, pair{n - 2, k})27 a[n-2] -= k * fi28 a[n-3] -= k * se29 }30 31 dp := make([][17][17]int, n)32 for i := range dp {33 for j := range dp[i] {34 for k := range dp[i][j] {35 dp[i][j][k] = -136 }37 }38 }39 var f func(int, int, int) int40 f = func(i, preHit, preHp int) int {41 if i == n-1 {42 return 043 }44 p := &dp[i][preHit][preHp]45 if *p != -1 {46 return *p47 }48 hp := a[i] - preHit*se49 hit := 050 if preHp > 0 {51 hit = (preHp-1)/se + 152 hp -= hit * fi53 }54 res := int(1e9)55 for {56 res = min(res, hit+f(i+1, hit, max(hp, 0)))57 if hp <= 0 && hit*se >= a[i+1] {58 *p = res59 return res60 }61 hit++62 hp -= fi63 }64 }65 66 var pr func(int, int, int)67 pr = func(i, preHit, preHp int) {68 if i == n-1 {69 return70 }71 hp := a[i] - preHit*se72 hit := 073 if preHp > 0 {74 hit = (preHp-1)/se + 175 hp -= hit * fi76 }77 res := f(i, preHit, preHp)78 for {79 if hit+f(i+1, hit, max(hp, 0)) == res {80 ans = append(ans, pair{i, hit})81 pr(i+1, hit, max(hp, 0))82 return83 }84 hit++85 hp -= fi86 }87 }88 pr(1, 0, 0)89 90 s := 091 for _, p := range ans {92 s += p.c93 }94 Fprintln(out, s)95 for _, p := range ans {96 for c := p.c; c > 0; c-- {97 Fprint(out, p.i+1, " ")98 }99 }100}101 102103