- Identify the ordered answer range or sorted search domain.
- Write a predicate whose truth changes only once.
- Move the appropriate boundary after each midpoint check and return the final feasible position.
Code notes
- 82 lines of Go from the credited upstream file 1680C.go.
- The implementation keeps its working state in language-native values and containers.
- 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.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7 "sort"8 "strings"9)10 1112func CF1680C(_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 int24 var s string25 for Fscan(in, &T); T > 0; T-- {26 Fscan(in, &s)27 ans := len(s)28 in0 := 029 out1 := strings.Count(s, "1")30 left := 031 for _, b := range s {32 v := int(b & 1)33 in0 += v ^ 134 out1 -= v35 for in0 > out1 {36 v = int(s[left] & 1)37 in0 -= v ^ 138 out1 += v39 left++40 }41 ans = min(ans, out1)42 }43 Fprintln(out, ans)44 }45}46 47func CF1680C_binarySearch(_r io.Reader, _w io.Writer) {48 in := bufio.NewReader(_r)49 out := bufio.NewWriter(_w)50 defer out.Flush()51 52 var T int53 var s string54 for Fscan(in, &T); T > 0; T-- {55 Fscan(in, &s)56 n := len(s)57 tot1 := strings.Count(s, "1")58 Fprintln(out, sort.Search(n, func(mx int) bool {59 in0 := 0 60 out1 := tot1 61 left := 062 for _, b := range s {63 v := int(b & 1)64 in0 += v ^ 165 out1 -= v66 for in0 > mx {67 v = int(s[left] & 1)68 in0 -= v ^ 169 out1 += v70 left++71 }72 if out1 <= mx {73 return true74 }75 }76 return false77 }))78 }79}80 8182