Approach
Sorting and greedy selection
For Codeforces 1367F2 — Flying Sort (Hard Version), 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
- 58 lines of Go from the credited upstream file 1367F2.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 . "fmt"5 "io"6 "slices"7 "sort"8)9 1011func cf1367F2(in io.Reader, out io.Writer) {12 var T, n int13 for Fscan(in, &T); T > 0; T-- {14 Fscan(in, &n)15 a := make([]int, n)16 for i := range a {17 Fscan(in, &a[i])18 }19 b := slices.Clone(a)20 slices.Sort(b)21 b = slices.Compact(b)22 23 m := len(b)24 tot := make([]int, m)25 for i, v := range a {26 a[i] = sort.SearchInts(b, v)27 tot[a[i]]++28 }29 30 31 f := make([]int, m)32 33 34 35 full := make([]int, m)36 cnt := make([]int, m)37 for _, v := range a {38 if v > 0 {39 if cnt[v-1] == tot[v-1] {40 f[v] = max(f[v], full[v-1])41 } else {42 f[v] = max(f[v], cnt[v-1])43 }44 }45 f[v]++ 46 if cnt[v] > 0 {47 full[v]++48 } else {49 full[v] = f[v]50 }51 cnt[v]++52 }53 Fprintln(out, n-slices.Max(f))54 }55}56 5758