- 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
- 52 lines of Go from the credited upstream file 772B.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"8)9 10type vec772 struct {11 x, y float6412}13 14func (a vec772) sub(b vec772) vec772 { return vec772{a.x - b.x, a.y - b.y} }15func (a vec772) len() float64 { return math.Hypot(a.x, a.y) }16func (a vec772) det(b vec772) float64 { return a.x*b.y - a.y*b.x }17 18type line struct {19 p1, p2 vec77220}21 22func (a vec772) disToLine(l line) float64 {23 v, u := l.p2.sub(l.p1), a.sub(l.p1)24 return math.Abs(v.det(u)) / v.len()25}26 2728func Sol772B(reader io.Reader, writer io.Writer) {29 in := bufio.NewReader(reader)30 out := bufio.NewWriter(writer)31 defer out.Flush()32 33 var n int34 Fscan(in, &n)35 ps := make([]vec772, n, n+2)36 for i := range ps {37 Fscan(in, &ps[i].x, &ps[i].y)38 }39 ps = append(ps, ps[0], ps[1])40 ans := math.MaxFloat6441 for i := 1; i <= n; i++ {42 if d := ps[i].disToLine(line{ps[i-1], ps[i+1]}); d < ans {43 ans = d44 }45 }46 Fprintf(out, "%.10f", ans/2)47}48 49505152