- 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
- 85 lines of Go from the credited upstream file 1017E.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 "cmp"5 . "fmt"6 "io"7 "slices"8)9 1011type vec struct{ x, y int }12 13func (a vec) sub(b vec) vec { return vec{a.x - b.x, a.y - b.y} }14func (a vec) det(b vec) int { return a.x*b.y - a.y*b.x }15func (a vec) len2() int { return a.x*a.x + a.y*a.y }16func (a vec) dis2(b vec) int { return b.sub(a).len2() }17 18func cf1017E(in io.Reader, out io.Writer) {19 smallestRepresentation := func(s []int) []int {20 n := len(s)21 s = append(s, s...)22 i := 023 for j := 1; j < n; {24 k := 025 for k < n && s[i+k] == s[j+k] {26 k++27 }28 if k >= n {29 break30 }31 if s[i+k] < s[j+k] {32 j += k + 133 } else {34 i, j = j, max(j, i+k)+135 }36 }37 return s[i : i+n]38 }39 convexHull := func(n int) []int {40 ps := make([]vec, n)41 for i := range ps {42 Fscan(in, &ps[i].x, &ps[i].y)43 }44 45 q := []vec{}46 slices.SortFunc(ps, func(a, b vec) int { return cmp.Or(a.x-b.x, a.y-b.y) })47 for _, p := range ps {48 for len(q) > 1 && q[len(q)-1].sub(q[len(q)-2]).det(p.sub(q[len(q)-1])) <= 0 {49 q = q[:len(q)-1]50 }51 q = append(q, p)52 }53 m := len(q)54 for i := len(ps) - 2; i >= 0; i-- {55 p := ps[i]56 for len(q) > m && q[len(q)-1].sub(q[len(q)-2]).det(p.sub(q[len(q)-1])) <= 0 {57 q = q[:len(q)-1]58 }59 q = append(q, p)60 }61 62 m = len(q)63 s := make([]int, 1, m*4-4)64 s[0] = q[0].dis2(q[1])65 for i := 2; i < m; i++ {66 s = append(s, -q[i-2].dis2(q[i]), q[i-1].dis2(q[i]))67 }68 s = append(s, -q[m-2].dis2(q[1]))69 70 return smallestRepresentation(s)71 }72 73 var n, m int74 Fscan(in, &n, &m)75 a := convexHull(n)76 b := convexHull(m)77 if slices.Equal(a, b) {78 Fprintln(out, "YES")79 } else {80 Fprintln(out, "NO")81 }82}83 8485