- 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
- 84 lines of Go from the credited upstream file 613A.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 10const eps13 = 1e-611 12type vec13 struct {13 x, y float6414}15 16func (a vec13) add(b vec13) vec13 { return vec13{a.x + b.x, a.y + b.y} }17func (a vec13) sub(b vec13) vec13 { return vec13{a.x - b.x, a.y - b.y} }18func (a vec13) mul(k float64) vec13 { return vec13{a.x * k, a.y * k} }19func (a vec13) len2() float64 { return a.x*a.x + a.y*a.y }20func (a vec13) dot(b vec13) float64 { return a.x*b.x + a.y*b.y }21func (a vec13) det(b vec13) float64 { return a.x*b.y - a.y*b.x }22 23type line613 struct {24 p1, p2 vec1325}26 27func (a vec13) perpendicular(l line613) line613 {28 return line613{a, a.add(vec13{l.p1.y - l.p2.y, l.p2.x - l.p1.x})}29}30 31func (a line613) intersection(b line613) vec13 {32 va, vb := a.p2.sub(a.p1), b.p2.sub(b.p1)33 k := vb.det(b.p1.sub(a.p1)) / vb.det(a.p2.sub(a.p1))34 return a.p1.add(va.mul(k))35}36 37func (a vec13) onSeg(l line613) bool {38 p1 := l.p1.sub(a)39 p2 := l.p2.sub(a)40 return math.Abs(p1.det(p2)) < eps13 && p1.dot(p2) < eps1341}42 43func (a vec13) disToSeg(l line613) float64 {44 p := l.intersection(a.perpendicular(l))45 if !p.onSeg(l) {46 if l.p2.sub(l.p1).dot(p.sub(l.p1)) < -eps13 {47 p = l.p148 } else {49 p = l.p250 }51 }52 return a.sub(p).len2()53}54 5556func Sol613A(_r io.Reader, out io.Writer) {57 in := bufio.NewReader(_r)58 var n int59 var o vec1360 Fscan(in, &n, &o.x, &o.y)61 minD2, maxD2 := 1e18, 0.062 ps := make([]vec13, n)63 for i := range ps {64 Fscan(in, &ps[i].x, &ps[i].y)65 ps[i] = ps[i].sub(o)66 if d2 := ps[i].len2(); d2 > maxD2 {67 maxD2 = d268 }69 }70 ls := make([]line613, n)71 for i := 0; i < n-1; i++ {72 ls[i] = line613{ps[i], ps[i+1]}73 }74 ls[n-1] = line613{ps[n-1], ps[0]}75 for _, l := range ls {76 if d2 := (vec13{0, 0}).disToSeg(l); d2 < minD2 {77 minD2 = d278 }79 }80 Fprintf(out, "%.18f", (maxD2-minD2)*math.Pi)81}82 8384