Approach
Sorting and greedy selection
For Codeforces 632D — Longest Subsequence, 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
- 49 lines of Go from the credited upstream file 632D.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 1011func cf632D(in io.Reader, _w io.Writer) {12 out := bufio.NewWriter(_w)13 defer out.Flush()14 var n, m, v int15 Fscan(in, &n, &m)16 ps := make([][]int, m+1)17 for i := 1; i <= n; i++ {18 if Fscan(in, &v); v <= m {19 ps[v] = append(ps[v], i)20 }21 }22 lcmSize := make([]int, m+1)23 tar := 124 for i := 1; i <= m; i++ {25 for j := i; j <= m; j += i {26 lcmSize[j] += len(ps[i])27 if lcmSize[j] > lcmSize[tar] {28 tar = j29 }30 }31 }32 ids := []int{}33 for d := 1; d*d <= tar; d++ {34 if tar%d == 0 {35 ids = append(ids, ps[d]...)36 if d*d < tar {37 ids = append(ids, ps[tar/d]...)38 }39 }40 }41 slices.Sort(ids)42 Fprintln(out, tar, len(ids))43 for _, v := range ids {44 Fprint(out, v, " ")45 }46}47 4849