Problem solution · Go

Codeforces 6D — Lizards and Basements 2

Codeforces 6D — Lizards and Basements 2: 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
103 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 6D — Lizards and Basements 2, 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

  • 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.

Source

Code and credit

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

Full codeCodeforces 6D — Lizards and Basements 2 · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io") // https://space.bilibili.com/206214func cf6D(in io.Reader, out io.Writer) {	var n, fi, se int	Fscan(in, &n, &fi, &se)	a := make([]int, n)	for i := range a {		Fscan(in, &a[i])		a[i]++	} 	k := (a[0]-1)/se + 1	type pair struct{ i, c int }	ans := []pair{{1, k}}	a[1] -= k * fi	a[2] -= k * se 	if a[n-1] > 0 {		k = (a[n-1]-1)/se + 1		ans = append(ans, pair{n - 2, k})		a[n-2] -= k * fi		a[n-3] -= k * se	} 	dp := make([][17][17]int, n)	for i := range dp {		for j := range dp[i] {			for k := range dp[i][j] {				dp[i][j][k] = -1			}		}	}	var f func(int, int, int) int	f = func(i, preHit, preHp int) int {		if i == n-1 {			return 0		}		p := &dp[i][preHit][preHp]		if *p != -1 {			return *p		}		hp := a[i] - preHit*se		hit := 0		if preHp > 0 {			hit = (preHp-1)/se + 1			hp -= hit * fi		}		res := int(1e9)		for {			res = min(res, hit+f(i+1, hit, max(hp, 0)))			if hp <= 0 && hit*se >= a[i+1] {				*p = res				return res			}			hit++			hp -= fi		}	} 	var pr func(int, int, int)	pr = func(i, preHit, preHp int) {		if i == n-1 {			return		}		hp := a[i] - preHit*se		hit := 0		if preHp > 0 {			hit = (preHp-1)/se + 1			hp -= hit * fi		}		res := f(i, preHit, preHp)		for {			if hit+f(i+1, hit, max(hp, 0)) == res {				ans = append(ans, pair{i, hit})				pr(i+1, hit, max(hp, 0))				return			}			hit++			hp -= fi		}	}	pr(1, 0, 0) 	s := 0	for _, p := range ans {		s += p.c	}	Fprintln(out, s)	for _, p := range ans {		for c := p.c; c > 0; c-- {			Fprint(out, p.i+1, " ")		}	}} //func main() { cf6D(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 ↗