- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 67 lines of Go from the credited upstream file 1608C.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.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 "bytes"6 . "fmt"7 "io"8 "sort"9)10 1112func CF1608C(_r io.Reader, _w io.Writer) {13 in := bufio.NewReader(_r)14 out := bufio.NewWriter(_w)15 defer out.Flush()16 min := func(a, b int) int {17 if a > b {18 return b19 }20 return a21 }22 max := func(a, b int) int {23 if b > a {24 return b25 }26 return a27 }28 29 var T, n int30 for Fscan(in, &T); T > 0; T-- {31 Fscan(in, &n)32 a := make([]struct{ x, y, i int }, n)33 for i := range a {34 Fscan(in, &a[i].x)35 }36 for i := range a {37 Fscan(in, &a[i].y)38 a[i].i = i39 }40 41 sort.Slice(a, func(i, j int) bool { return a[i].x < a[j].x })42 suf := make([]int, n)43 suf[n-1] = a[n-1].y44 mn := a[n-1].y45 for i := n - 2; i >= 0; i-- {46 mn = min(mn, a[i].y)47 if a[i].y > suf[i+1] {48 suf[i] = mn49 } else {50 suf[i] = suf[i+1]51 }52 }53 54 ans := bytes.Repeat([]byte{'1'}, n)55 pre := 056 for i, p := range a[:n-1] {57 pre = max(pre, p.y)58 if pre < suf[i+1] {59 ans[p.i] = '0'60 }61 }62 Fprintf(out, "%s\n", ans)63 }64}65 6667