Problem solution · Go

Codeforces 1321E

Codeforces 1321E: 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
121 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 1321E, 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

  • 121 lines of Go from the credited upstream file 1321E.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 1321E · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"sort") // github.com/EndlessCheng/codeforces-go type pair1321 struct{ val, cost int }type node1321 struct{ l, r, max, todo int }type seg1321 []node1321 func (seg1321) max(a, b int) int {	if a > b {		return a	}	return b} func (t seg1321) _pushUp(o int) { t[o].max = t.max(t[o<<1].max, t[o<<1|1].max) } func (t seg1321) _build(a []pair1321, o, l, r int) {	t[o].l, t[o].r = l, r	if l == r {		t[o].max = -a[l-1].cost		return	}	m := (l + r) >> 1	t._build(a, o<<1, l, m)	t._build(a, o<<1|1, m+1, r)	t._pushUp(o)} func (t seg1321) _spread(o int) {	if add := t[o].todo; add != 0 {		lo, ro := &t[o<<1], &t[o<<1|1]		lo.max += add		ro.max += add		lo.todo += add		ro.todo += add		t[o].todo = 0	}} func (t seg1321) _update(o, l, r, add int) {	ol, or := t[o].l, t[o].r	if l <= ol && or <= r {		t[o].max += add		t[o].todo += add		return	}	t._spread(o)	m := (ol + or) >> 1	if l <= m {		t._update(o<<1, l, r, add)	}	if m < r {		t._update(o<<1|1, l, r, add)	}	t._pushUp(o)} func (t seg1321) init(a []pair1321)    { t._build(a, 1, 1, len(a)) }func (t seg1321) update(l, r, val int) { t._update(1, l, r, val) }func (t seg1321) maxAll() int          { return t[1].max } func CF1321E(_r io.Reader, _w io.Writer) {	in := bufio.NewScanner(_r)	in.Split(bufio.ScanWords)	read := func() (x int) {		in.Scan()		for _, b := range in.Bytes() {			x = x*10 + int(b-'0')		}		return	} 	n, m, p := read(), read(), read()	a := make([]pair1321, n)	for i := range a {		a[i] = pair1321{read(), read()}	}	sort.Slice(a, func(i, j int) bool { return a[i].val < a[j].val })	b := make([]pair1321, m)	for i := range b {		b[i] = pair1321{read(), read()}	}	sort.Slice(b, func(i, j int) bool { return b[i].val < b[j].val })	t := make(seg1321, 4*m)	t.init(b)	type monster struct{ x, y, coins int }	monsters := make([]monster, p)	for i := range monsters {		monsters[i] = monster{read(), read(), read()}	}	sort.Slice(monsters, func(i, j int) bool { return monsters[i].x < monsters[j].x }) 	ans := int(-2e9)	i := 0	for _, weapon := range a {		for ; i < p; i++ {			mst := monsters[i]			if mst.x >= weapon.val {				break			}			if minArmourI := sort.Search(m, func(i int) bool { return b[i].val > mst.y }); minArmourI < m {				t.update(minArmourI+1, m, mst.coins)			}		}		if profit := t.maxAll() - weapon.cost; profit > ans {			ans = profit		}	}	Fprint(_w, ans)} //func main() { CF1321E(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 ↗