Problem solution · Go

Codeforces 940F — Machine Learning

Codeforces 940F — Machine Learning: 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
117 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 940F — Machine Learning, 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

  • 117 lines of Go from the credited upstream file 940F.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 940F — Machine Learning · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"math"	"sort") // github.com/EndlessCheng/codeforces-gofunc CF940F(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush() 	var n, q, op, pos, v int	Fscan(in, &n, &q)	rk := map[int]int{}	a := make([]int, n+1)	for i := 1; i <= n; i++ {		Fscan(in, &v)		if rk[v] == 0 {			rk[v] = len(rk) + 1		}		a[i] = rk[v]	}	B := int(math.Round(math.Pow(float64(n), 2.0/3)))	type query struct{ lb, rb, l, r, t, qid int }	type modify struct{ pos, val int }	qs := []query{}	ms := []modify{}	for ; q > 0; q-- {		if Fscan(in, &op); op == 1 {			var l, r int			Fscan(in, &l, &r)			qs = append(qs, query{l / B, (r + 1) / B, l, r + 1, len(ms), len(qs)})		} else {			Fscan(in, &pos, &v)			if rk[v] == 0 {				rk[v] = len(rk) + 1			}			ms = append(ms, modify{pos, rk[v]})		}	}	sort.Slice(qs, func(i, j int) bool {		a, b := qs[i], qs[j]		if a.lb != b.lb {			return a.lb < b.lb		}		if a.rb != b.rb {			return a.rb < b.rb		}		if a.rb&1 == 0 {			return a.t < b.t		}		return a.t > b.t	}) 	cnt := make([]int, len(rk)+1)	cc := make([]int, n+2) // 求 mex 要多开一个空间	l, r, now := 1, 1, 0	add := func(val int) {		cc[cnt[val]]--		cnt[val]++		cc[cnt[val]]++	}	del := func(val int) {		cc[cnt[val]]--		cnt[val]--		cc[cnt[val]]++	}	ans := make([]int, len(qs))	for _, q := range qs {		for ; r < q.r; r++ {			add(a[r])		}		for ; l < q.l; l++ {			del(a[l])		}		for l > q.l {			l--			add(a[l])		}		for r > q.r {			r--			del(a[r])		}		for ; now < q.t; now++ {			m := ms[now]			p, v := m.pos, m.val			if q.l <= p && p < q.r {				del(a[p])				add(v)			}			a[p], ms[now].val = v, a[p]		}		for now > q.t {			now--			m := ms[now]			p, v := m.pos, m.val			if q.l <= p && p < q.r {				del(a[p])				add(v)			}			a[p], ms[now].val = v, a[p]		}		for ans[q.qid] = 1; cc[ans[q.qid]] > 0; ans[q.qid]++ {		}	}	for _, v := range ans {		Fprintln(out, v)	}} //func main() { CF940F(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 ↗