Problem solution · Go

Codeforces 1179C — Serge and Dining Room

Codeforces 1179C — Serge and Dining Room: a Go solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Binary search
Source
EndlessCheng Codeforces Go
Length
89 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

Binary search

For Codeforces 1179C — Serge and Dining Room, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 89 lines of Go from the credited upstream file 1179C.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

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 1179C — Serge and Dining Room · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") // github.com/EndlessCheng/codeforces-gotype seg79 []struct{ l, r, s, mx int } func (t seg79) build(o, l, r int) {	t[o].l, t[o].r = l, r	if l == r {		return	}	m := (l + r) >> 1	t.build(o<<1, l, m)	t.build(o<<1|1, m+1, r)} func (t seg79) update(o, i, v int) {	if t[o].l == t[o].r {		t[o].s += v		t[o].mx += v		return	}	m := (t[o].l + t[o].r) >> 1	if i <= m {		t.update(o<<1, i, v)	} else {		t.update(o<<1|1, i, v)	}	lo, ro := t[o<<1], t[o<<1|1]	t[o].s = lo.s + ro.s	t[o].mx = max(lo.mx+ro.s, ro.mx)} func (t seg79) binarySearch(o, s int) int {	if t[o].l == t[o].r {		if t[o].s > s {			return t[o].l		}		return -1	}	if t[o<<1|1].mx > s {		return t.binarySearch(o<<1|1, s)	}	return t.binarySearch(o<<1, s-t[o<<1|1].s)} func CF1179C(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush()	const mx int = 1e6 	t := make(seg79, 4*mx)	t.build(1, 1, mx)	var n, m, q, op, p, v int	Fscan(in, &n, &m)	a := make([]int, n)	for i := range a {		Fscan(in, &a[i])		t.update(1, a[i], 1) // 类似括号匹配,如此更新后,就可以从后往前找第一个大于 0 的位置了	}	b := make([]int, m)	for i := range b {		Fscan(in, &b[i])		t.update(1, b[i], -1)	}	for Fscan(in, &q); q > 0; q-- {		Fscan(in, &op, &p, &v)		p--		if op == 1 {			t.update(1, a[p], -1)			t.update(1, v, 1)			a[p] = v		} else {			t.update(1, b[p], 1)			t.update(1, v, -1)			b[p] = v		}		Fprintln(out, t.binarySearch(1, 0))	}} //func main() { CF1179C(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 ↗