Problem solution · Go

Codeforces 743E — Vladik and cards

Codeforces 743E — Vladik and cards: a Go solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Dynamic programming
Source
EndlessCheng Codeforces Go
Length
78 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For Codeforces 743E — Vladik and cards, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. 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.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 743E — Vladik and cards · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"math/bits"	"sort") // github.com/EndlessCheng/codeforces-gofunc CF743E(_r io.Reader, out io.Writer) {	in := bufio.NewReader(_r)	min := func(a, b int) int {		if a < b {			return a		}		return b	}	const mx = 8	const m = 1 << mx 	var n, v int	Fscan(in, &n)	pos := [mx][]int{}	for i := 1; i <= n; i++ {		Fscan(in, &v)		pos[v-1] = append(pos[v-1], i)	} 	minC := n	for _, ps := range pos {		minC = min(minC, len(ps))	}	// 注:更快的做法是二分 c	for c := minC + 1; ; c-- { // 当前枚举的每个数的个数必须是 c 或 c-1		// 状态定义为连续元素为 mask 下,最后一个元素的下标的最小值		// 第二维度表示 c-1 的出现次数		dp := make([][mx]int, m)		for i := 1; i < m; i++ {			for j := range dp[i] {				dp[i][j] = 1e9			}		}		for s, dv := range dp {			for t, lb := m-1^s, 0; t > 0; t ^= lb {				lb = t & -t				ss := s | lb				ps := pos[bits.TrailingZeros(uint(lb))]				for k, p := range dv {					i := sort.SearchInts(ps, p+1) + c - 1 // 取 c 个数后的位置					if i-1 < len(ps) {						if i < len(ps) {							dp[ss][k] = min(dp[ss][k], ps[i])						}						if k+1 < mx {							// 取 c-1 个数的位置,特判 c=1 的情况							if c == 1 {								dp[ss][k+1] = min(dp[ss][k+1], p)							} else {								dp[ss][k+1] = min(dp[ss][k+1], ps[i-1])							}						}					}				}			}		}		for i, p := range dp[m-1] {			if p <= n {				Fprint(out, c*mx-i)				return			}		}	}} //func main() { CF743E(os.Stdin, os.Stdout) } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗