Approach
Sorting and greedy selection
For Codeforces 1991F — Triangle Formation, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 61 lines of Go from the credited upstream file 1991F.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
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 "slices"8)9 10func cf1991F(in io.Reader, _w io.Writer) {11 out := bufio.NewWriter(_w)12 defer out.Flush()13 var n, q, l int14 Fscan(in, &n, &q)15 a := make([]int32, n)16 for i := range a {17 Fscan(in, &a[i])18 }19 20 ok := func(l, r int) bool {21 if r-l < 6 {22 return false23 }24 b := slices.Clone(a[l:r])25 slices.Sort(b)26 for i := 2; i < len(b); i++ {27 for j := 0; j < i-1; j++ {28 if b[j]+b[i-1] <= b[i] {29 continue30 }31 c := append(slices.Clone(b[j+1:i-1]), b[i+1:]...)32 for k := 2; k < len(c); k++ {33 if c[k-2]+c[k-1] > c[k] {34 return true35 }36 }37 }38 }39 return false40 }41 minR := make([]int, n)42 r := 643 for i := range minR {44 for r <= n && !ok(i, r) {45 r++46 }47 minR[i] = r48 }49 50 for ; q > 0; q-- {51 Fscan(in, &l, &r)52 if r < minR[l-1] {53 Fprintln(out, "NO")54 } else {55 Fprintln(out, "YES")56 }57 }58}59 6061