- 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
- 64 lines of Go from the credited upstream file 1133E.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 "sort"8)9 10func Sol1133E(reader io.Reader, writer io.Writer) {11 max := func(a, b int) int {12 if a >= b {13 return a14 }15 return b16 }17 18 in := bufio.NewReader(reader)19 out := bufio.NewWriter(writer)20 defer out.Flush()21 22 const maxTeamDelta = 523 24 var n, k int25 Fscan(in, &n, &k)26 arr := make([]int, n+1)27 for i := 1; i <= n; i++ {28 Fscan(in, &arr[i])29 }30 sort.Ints(arr)31 32 deltaPos := make([][maxTeamDelta + 1]int, n+1)33 for j := 0; j <= maxTeamDelta; j++ {34 deltaPos[1][j] = 135 }36 for i := 2; i <= n; i++ {37 for j := 0; j <= maxTeamDelta; j++ {38 deltaPos[i][j] = i39 }40 if delta := arr[i] - arr[i-1]; delta <= maxTeamDelta {41 copy(deltaPos[i][delta:], deltaPos[i-1][:maxTeamDelta+1-delta])42 }43 }44 45 var dp [2][5001]int46 for j := 1; j <= n; j++ {47 dp[0][j] = max(dp[0][j-1], j-deltaPos[j][maxTeamDelta]+1)48 }49 row := 050 for i := 2; i <= k; i++ {51 row ^= 152 for j := i; j <= n; j++ {53 posL := deltaPos[j][maxTeamDelta]54 55 dp[row][j] = max(dp[row][j-1], dp[row^1][posL-1]+j-posL+1)56 }57 }58 Fprintln(out, max(dp[row][n], dp[row^1][n]))59}60 61626364