- 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
- 60 lines of Go from the credited upstream file 1626D.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 . "fmt"6 "io"7 "math/bits"8 "sort"9)10 1112func CF1626D(_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 23 var T, n, v int24 for Fscan(in, &T); T > 0; T-- {25 Fscan(in, &n)26 cnt := make([]int, n+1)27 for i := 0; i < n; i++ {28 Fscan(in, &v)29 cnt[v]++30 }31 for i := 0; i < n; i++ {32 cnt[i+1] += cnt[i]33 }34 35 ans := int(1e9)36 for i, c := range cnt {37 for k := 1; k < n*2; k <<= 1 {38 j := sort.SearchInts(cnt[:i+1], k+1) - 139 c1 := cnt[j]40 c2 := c - c141 c3 := cnt[n] - c42 x1, x2, x3 := 1, 1, 143 if c1 > 0 {44 x1 = 1<<bits.Len(uint(c1-1)) - c145 }46 if c2 > 0 {47 x2 = 1<<bits.Len(uint(c2-1)) - c248 }49 if c3 > 0 {50 x3 = 1<<bits.Len(uint(c3-1)) - c351 }52 ans = min(ans, x1+x2+x3)53 }54 }55 Fprintln(out, ans)56 }57}58 5960