Approach
Sorting and greedy selection
For Codeforces 1896C — Matching Arrays, 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
- 55 lines of Go from the credited upstream file 1896C.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 cf1896C(in io.Reader, _w io.Writer) {12 out := bufio.NewWriter(_w)13 defer out.Flush()14 var T, n, x int15o:16 for Fscan(in, &T); T > 0; T-- {17 Fscan(in, &n, &x)18 type pair struct{ v, i int }19 a := make([]pair, n)20 for i := range a {21 Fscan(in, &a[i].v)22 a[i].i = i23 }24 slices.SortFunc(a, func(a, b pair) int { return a.v - b.v })25 b := make([]int, n)26 for i := range b {27 Fscan(in, &b[i])28 }29 slices.Sort(b)30 31 for i, v := range b[x:] {32 if a[i].v > v {33 Fprintln(out, "NO")34 continue o35 }36 }37 for i, p := range a[n-x:] {38 if p.v <= b[i] {39 Fprintln(out, "NO")40 continue o41 }42 }43 44 Fprintln(out, "YES")45 b = append(b[x:], b[:x]...)46 ans := make([]any, n)47 for i, v := range b {48 ans[a[i].i] = v49 }50 Fprintln(out, ans...)51 }52}53 5455