- 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
- 55 lines of Go from the credited upstream file 1299C.go.
- The implementation visibly relies on sequence storage.
- 1 loop block 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)8 9type vec1299 struct{ x, y int64 }10 11func (a vec1299) sub(b vec1299) vec1299 { return vec1299{a.x - b.x, a.y - b.y} }12func (a vec1299) det(b vec1299) int64 { return a.x*b.y - a.y*b.x }13 1415func CF1299C(_r io.Reader, _w io.Writer) {16 in := bufio.NewScanner(_r)17 in.Split(bufio.ScanWords)18 out := bufio.NewWriter(_w)19 defer out.Flush()20 read := func() (x int) {21 in.Scan()22 for _, b := range in.Bytes() {23 x = x*10 + int(b-'0')24 }25 return26 }27 28 n := read()29 s := make([]int64, n+1)30 ps := make([]vec1299, 1, n+1)31 for i := 1; i <= n; i++ {32 s[i] = s[i-1] + int64(read())33 p := vec1299{int64(i), s[i]}34 for {35 sz := len(ps)36 if sz <= 1 || ps[sz-1].sub(ps[sz-2]).det(p.sub(ps[sz-1])) > 0 {37 break38 }39 ps = ps[:sz-1]40 }41 ps = append(ps, p)42 }43 sz := len(ps)44 for i := 1; i < sz; i++ {45 a, b := ps[i-1], ps[i]46 l := b.x - a.x47 avg := float64(b.y-a.y) / float64(l)48 for ; l > 0; l-- {49 Fprintf(out, "%.9f\n", avg)50 }51 }52}53 5455