Problem solution · Go

Codeforces 1102F — Elongated Matrix

Codeforces 1102F — Elongated Matrix: 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
100 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 1102F — Elongated Matrix, 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

  • 100 lines of Go from the credited upstream file 1102F.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 1102F — Elongated Matrix · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"math/bits") // github.com/EndlessCheng/codeforces-gofunc CF1102F(_r io.Reader, out io.Writer) {	in := bufio.NewReader(_r)	abs := func(x int) int {		if x < 0 {			return -x		}		return x	}	min := func(a, b int) int {		if a < b {			return a		}		return b	}	max := func(a, b int) int {		if a > b {			return a		}		return b	} 	var n, m, ans int	Fscan(in, &n, &m)	a := make([][]int, n)	minD := make([][]int, n)	for i := range minD {		minD[i] = make([]int, n)		for j := range minD[i] {			minD[i][j] = 1e9		}	}	for i := range a {		a[i] = make([]int, m)		for j := range a[i] {			Fscan(in, &a[i][j])			for k := 0; k < i; k++ {				minD[i][k] = min(minD[i][k], abs(a[i][j]-a[k][j]))				minD[k][i] = minD[i][k]			}		}	}	if n == 1 {		ans = 1e9		for i, v := range a[0][:m-1] {			ans = min(ans, abs(v-a[0][i+1]))		}		Fprint(out, ans)		return	} 	dp := make([][]int, 1<<n)	for i := range dp {		dp[i] = make([]int, n)	}	for stI, stR := range a {		for _, r := range dp {			for j := range r {				r[j] = 0			}		}		for i, mi := range minD[stI] {			if i != stI {				dp[1<<i][i] = mi			}		}		for s := 1; s < len(dp); s++ {			for ss := uint(s); ss > 0; ss &= ss - 1 {				i := bits.TrailingZeros(ss)				for t, lb := 1<<n-1^s, 0; t > 0; t ^= lb {					lb = t & -t					if j := bits.TrailingZeros(uint(lb)); j != stI {						dp[s|lb][j] = max(dp[s|lb][j], min(dp[s][i], minD[i][j]))					}				}			}		}		for endI, dv := range dp[1<<n-1^(1<<stI)] {			if endI != stI {				for k, v := range a[endI][:m-1] {					dv = min(dv, abs(v-stR[k+1]))				}				ans = max(ans, dv)			}		}	}	Fprint(out, ans)} //func main() { CF1102F(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 ↗