Problem solution · Go

Codeforces 1826E — Walk the Runway

Codeforces 1826E — Walk the Runway: 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
119 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 1826E — Walk the Runway, 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

  • 119 lines of Go from the credited upstream file 1826E.go.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • 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 1826E — Walk the Runway · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io"	"math"	"math/bits"	"sort") // https://space.bilibili.com/206214const _w26 = bits.UintSize func NewBitset26(n int) Bitset26 { return make(Bitset26, n/_w26+1) } // (n+_w-1)/_w type Bitset26 []uint func (b Bitset26) Has(p int) bool { return b[p/_w26]&(1<<(p%_w26)) != 0 } // getfunc (b Bitset26) Set(p int)      { b[p/_w26] |= 1 << (p % _w26) }        // 置 1func (b Bitset26) SetAll1() {	for i := range b {		b[i] = math.MaxUint	}}func (b Bitset26) IntersectionFrom(c Bitset26) {	for i, v := range c {		b[i] &= v	}} func CF1826E(_r io.Reader, out io.Writer) {	_i, _n, buf := 0, 0, make([]byte, 1<<12)	rc := func() byte {		if _i == _n {			_n, _ = _r.Read(buf)			if _n == 0 {				return 0			}			_i = 0		}		b := buf[_i]		_i++		return b	}	ri := func() (x int) {		b := rc()		for ; '0' > b; b = rc() {		}		for ; '0' <= b; b = rc() {			x = x*10 + int(b&15)		}		return	}	max := func(a, b int64) int64 {		if b > a {			return b		}		return a	} 	m, n := ri(), ri()	type pair struct {		p int		r []int	}	a := make([]pair, n)	for i := range a {		a[i].p = ri()		a[i].r = make([]int, m)	} 	for i := 0; i < m; i++ {		for _, p := range a {			p.r[i] = ri()		}	} 	sort.Slice(a, func(i, j int) bool { return a[i].r[0] < a[j].r[0] }) 	from := make([]Bitset26, n)	for i := range from {		from[i] = NewBitset26(n)		from[i].SetAll1()	} 	ids := make([]int, n)	for i := range ids {		ids[i] = i	}	for city := 0; city < m; city++ {		sort.Slice(ids, func(i, j int) bool { return a[ids[i]].r[city] < a[ids[j]].r[city] })		cur := NewBitset26(n)		j := 0		for _, i := range ids {			for a[ids[j]].r[city] < a[i].r[city] {				cur.Set(ids[j])				j++			}			from[i].IntersectionFrom(cur)		}	} 	ans := int64(0)	f := make([]int64, n)	for i, p := range a {		f[i] = 0		for j := i - 1; j >= 0; j-- {			if f[j] > f[i] && from[i].Has(j) {				f[i] = f[j]			}		}		f[i] += int64(p.p)		ans = max(ans, f[i])	}	Fprint(out, ans)} //func main() { CF1826E(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 ↗