- 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
- 71 lines of Go from the credited upstream file 671A.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 "sort"9)10 11type vec71 struct {12 x, y int13}14 15func (a vec71) sub(b vec71) vec71 { return vec71{a.x - b.x, a.y - b.y} }16func (a vec71) len() float64 { return math.Hypot(float64(a.x), float64(a.y)) }17 1819func cf671A(reader io.Reader, writer io.Writer) {20 type pair struct {21 d float6422 idx int23 }24 read := func(in io.Reader) vec71 {25 var x, y int26 Fscan(in, &x, &y)27 return vec71{x, y}28 }29 in := bufio.NewReader(reader)30 out := bufio.NewWriter(writer)31 defer out.Flush()32 33 pa, pb, bin := read(in), read(in), read(in)34 pa = pa.sub(bin)35 pb = pb.sub(bin)36 var n int37 Fscan(in, &n)38 das := make([]pair, n)39 dbs := make([]pair, n)40 ans := 0.041 for i := range das {42 p := read(in).sub(bin)43 lenP := p.len()44 ans += lenP45 das[i] = pair{lenP - p.sub(pa).len(), i}46 dbs[i] = pair{lenP - p.sub(pb).len(), i}47 }48 ans *= 249 50 sort.Slice(das, func(i, j int) bool { return das[i].d > das[j].d })51 sort.Slice(dbs, func(i, j int) bool { return dbs[i].d > dbs[j].d })52 if n == 1 || das[0].d <= 0 || dbs[0].d <= 0 {53 ans -= max(das[0].d, dbs[0].d)54 } else if das[0].idx == dbs[0].idx {55 sum1 := das[0].d56 sum2 := dbs[0].d57 if dbs[1].d > 0 {58 sum1 += dbs[1].d59 }60 if das[1].d > 0 {61 sum2 += das[1].d62 }63 ans -= max(sum1, sum2)64 } else {65 ans -= das[0].d + dbs[0].d66 }67 Fprintf(out, "%.12f", ans)68}69 7071