- 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
- 65 lines of Go from the credited upstream file 1479A.go.
- The implementation keeps its working state in language-native values and containers.
- 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 "os"7 "sort"8)9 10type (11 input79 struct{ n int }12 req79 struct{ i int }13 resp79 struct{ v int }14 guess79 struct{ ans int }15)16 1718func CF1479A(in input79, Q func(req79) resp79) (gs guess79) {19 q := func(i int) int { return Q(req79{i}).v }20 n := in.n21 if n == 1 || q(1) < q(2) {22 gs.ans = 123 return24 }25 if q(n-1) > q(n) {26 gs.ans = n27 return28 }29 res := sort.Search(n, func(i int) bool {30 if i < 2 || gs.ans > 0 {31 return false32 }33 x, y, z := q(i-1), q(i), q(i+1)34 if x > y && y < z {35 gs.ans = i36 return false37 }38 return x < y && y < z39 })40 if gs.ans == 0 {41 gs.ans = res42 }43 return44}45 46func ioq79() {47 in := bufio.NewReader(os.Stdin)48 out := bufio.NewWriter(os.Stdout)49 50 Q := func(req req79) (resp resp79) {51 Fprintln(out, "?", req.i)52 out.Flush()53 Fscan(in, &resp.v)54 return55 }56 57 d := input79{}58 Fscan(in, &d.n)59 gs := CF1479A(d, Q)60 Fprint(out, "! ", gs.ans)61 out.Flush()62}63 6465