Problem solution · Go

Codeforces 1579E2 — Array Optimization by Deque

Codeforces 1579E2 — Array Optimization by Deque: 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
155 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 1579E2 — Array Optimization by Deque, 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

  • 155 lines of Go from the credited upstream file 1579E2.go.
  • The implementation keeps its working state in language-native values and containers.
  • 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 1579E2 — Array Optimization by Deque · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"time") // https://space.bilibili.com/206214type node79 struct {	lr       [2]*node79	priority uint	key      int	keyCnt   int	subCnt   int} func (o *node79) size() int {	if o != nil {		return o.subCnt	}	return 0} func (o *node79) maintain() {	o.subCnt = o.keyCnt + o.lr[0].size() + o.lr[1].size()} func (o *node79) rotate(d int) *node79 {	x := o.lr[d^1]	o.lr[d^1] = x.lr[d]	x.lr[d] = o	o.maintain()	x.maintain()	return x} type treap79 struct {	rd   uint	root *node79} func (t *treap79) fastRand() uint {	t.rd ^= t.rd << 13	t.rd ^= t.rd >> 17	t.rd ^= t.rd << 5	return t.rd} func (t *treap79) size() int { return t.root.size() } func (t *treap79) _put(o *node79, key int) *node79 {	if o == nil {		o = &node79{priority: t.fastRand(), key: key, keyCnt: 1}	} else if d := o.cmp(key); d >= 0 {		o.lr[d] = t._put(o.lr[d], key)		if o.lr[d].priority > o.priority {			o = o.rotate(d ^ 1)		}	} else {		o.keyCnt++	}	o.maintain()	return o} func (t *treap79) put(key int) { t.root = t._put(t.root, key) } func (t *treap79) _delete(o *node79, key int) *node79 {	if o == nil {		return nil	}	if d := o.cmp(key); d >= 0 {		o.lr[d] = t._delete(o.lr[d], key)	} else {		if o.keyCnt > 1 {			o.keyCnt--		} else {			if o.lr[1] == nil {				return o.lr[0]			}			if o.lr[0] == nil {				return o.lr[1]			}			d = 0			if o.lr[0].priority > o.lr[1].priority {				d = 1			}			o = o.rotate(d)			o.lr[d] = t._delete(o.lr[d], key)		}	}	o.maintain()	return o} func (t *treap79) delete(key int) { t.root = t._delete(t.root, key) } func (o *node79) cmp(a int) int {	b := o.key	if a == b {		return -1	}	if a < b {		return 0	}	return 1} func (t *treap79) rank(key int) (kth int) {	for o := t.root; o != nil; {		switch c := o.cmp(key); {		case c == 0:			o = o.lr[0]		case c > 0:			kth += o.lr[0].size() + o.keyCnt			o = o.lr[1]		default:			kth += o.lr[0].size()			return		}	}	return} func CF1579E2(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush()	min := func(a, b int) int {		if a > b {			return b		}		return a	} 	var T, n, v int	t := &treap79{rd: uint(time.Now().UnixNano())/2 + 1}	for Fscan(in, &T); T > 0; T-- {		ans := int64(0)		Fscan(in, &n, &v)		t.root = nil		t.put(v)		for i := 1; i < n; i++ {			Fscan(in, &v)			ans += int64(min(t.rank(v), i-t.rank(v+1)))			t.put(v)		}		Fprintln(out, ans)	}} //func main() { CF1579E2(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 ↗