- 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
- 71 lines of Go from the credited upstream file 796E.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 . "fmt"5 "io"6)7 89func cf796E(in io.Reader, out io.Writer) {10 var n, p, k, m, v, ans int11 Fscan(in, &n, &p, &k)12 done := make([][2]bool, n+1)13 for i := range 2 {14 Fscan(in, &m)15 for range m {16 Fscan(in, &v)17 done[v][i] = true18 }19 }20 for _, p := range done {21 if p[0] || p[1] {22 ans++23 }24 }25 if p/2*k >= n {26 Fprint(out, ans)27 return28 }29 30 dp := make([][][][2]int16, k+1)31 for i := range dp {32 dp[i] = make([][][2]int16, p+1)33 for j := range dp[i] {34 dp[i][j] = make([][2]int16, n+1)35 for l := range dp[i][j] {36 dp[i][j][l] = [2]int16{-1, -1}37 }38 }39 }40 var f func(int, int, int, int) int1641 f = func(i, p, ex, who int) (miss int16) {42 if i <= 0 {43 return44 }45 t := &dp[ex][p][i][who]46 if *t >= 0 {47 return *t48 }49 50 miss = f(i-1, p, max(ex-1, 0), who)51 d := done[i]52 if ex == 0 && (d[0] || d[1]) || ex > 0 && !d[who] && d[who^1] {53 miss++54 }55 56 if p > 0 {57 if ex == 0 {58 miss = min(miss, f(i, p-1, k, 0), f(i, p-1, k, 1))59 } else {60 miss = min(miss, f(i-ex, p-1, k-ex, who^1))61 }62 }63 64 *t = miss65 return66 }67 Fprint(out, ans-int(f(n, p, 0, 0)))68}69 7071