Problem solution · Go

Codeforces 991D — Bishwock

Codeforces 991D — Bishwock: a Go solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Direct simulation
Source
EndlessCheng Codeforces Go
Length
40 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

Direct simulation

For Codeforces 991D — Bishwock, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 40 lines of Go from the credited upstream file 991D.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

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 991D — Bishwock · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") // https://space.bilibili.com/206214func CF991D(in io.Reader, out io.Writer) {	max := func(a, b int) int {		if b > a {			return b		}		return a	}	var s1, s2 []byte	Fscan(bufio.NewReader(in), &s1, &s2)	const inf int = 1e9	shape := [][3]int{{3, 1}, {1, 3}, {2, 3}, {3, 2}} // 依次对应题目描述的 4 种 L 形状 	f := [4]int{-inf, -inf, -inf, 0} // 第 -1 列视作都是 X	for i, b := range s1 {		cur := int(b>>6 | s2[i]>>6<<1) // 第 i 列的 X		nf := [4]int{-inf, -inf, -inf, -inf}		nf[cur] = max(max(max(f[0], f[1]), f[2]), f[3]) // 不填 L		for _, p := range shape { // 填 L,枚举 L 形状			for pre := 0; pre < 4; pre++ { // 枚举第 i-1 列				if p[0]&pre == 0 && p[1]&cur == 0 { // 可以填 L					nf[p[1]|cur] = max(nf[p[1]|cur], f[pre]+1)				}			}		}		f = nf	}	Fprint(out, max(max(max(f[0], f[1]), f[2]), f[3]))} //func main() { CF991D(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 ↗