- 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
- 78 lines of Go from the credited upstream file 743E.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/bits"8 "sort"9)10 1112func CF743E(_r io.Reader, out io.Writer) {13 in := bufio.NewReader(_r)14 min := func(a, b int) int {15 if a < b {16 return a17 }18 return b19 }20 const mx = 821 const m = 1 << mx22 23 var n, v int24 Fscan(in, &n)25 pos := [mx][]int{}26 for i := 1; i <= n; i++ {27 Fscan(in, &v)28 pos[v-1] = append(pos[v-1], i)29 }30 31 minC := n32 for _, ps := range pos {33 minC = min(minC, len(ps))34 }35 36 for c := minC + 1; ; c-- { 37 38 39 dp := make([][mx]int, m)40 for i := 1; i < m; i++ {41 for j := range dp[i] {42 dp[i][j] = 1e943 }44 }45 for s, dv := range dp {46 for t, lb := m-1^s, 0; t > 0; t ^= lb {47 lb = t & -t48 ss := s | lb49 ps := pos[bits.TrailingZeros(uint(lb))]50 for k, p := range dv {51 i := sort.SearchInts(ps, p+1) + c - 1 52 if i-1 < len(ps) {53 if i < len(ps) {54 dp[ss][k] = min(dp[ss][k], ps[i])55 }56 if k+1 < mx {57 58 if c == 1 {59 dp[ss][k+1] = min(dp[ss][k+1], p)60 } else {61 dp[ss][k+1] = min(dp[ss][k+1], ps[i-1])62 }63 }64 }65 }66 }67 }68 for i, p := range dp[m-1] {69 if p <= n {70 Fprint(out, c*mx-i)71 return72 }73 }74 }75}76 7778