Approach
Sorting and greedy selection
For Codeforces 1735E — House Planning, 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
- 83 lines of Go from the credited upstream file 1735E.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 cf1735E(in io.Reader, _w io.Writer) {12 abs := func(x int) int {13 if x < 0 {14 return -x15 }16 return x17 }18 out := bufio.NewWriter(_w)19 defer out.Flush()20 var T, n int21o:22 for Fscan(in, &T); T > 0; T-- {23 Fscan(in, &n)24 a := make([]int, n)25 for i := range a {26 Fscan(in, &a[i])27 }28 slices.Sort(a)29 b := make([]int, n)30 for i := range b {31 Fscan(in, &b[i])32 }33 34 f := func(k int) bool {35 cnt := map[int]int{}36 for _, v := range b {37 cnt[v]++38 }39 tp := make([]int8, n)40 for i := n - 1; i >= 0; i-- {41 x := a[i] + k42 y := abs(a[i] - k)43 if c, _ := cnt[x]; c > 0 {44 cnt[x]--45 tp[i] = 146 continue47 }48 if c, _ := cnt[y]; c > 0 {49 cnt[y]--50 tp[i] = 251 continue52 }53 return false54 }55 56 Fprintln(out, "YES")57 p := int(1e9)58 if k > 1e9 {59 p = 2e9 - k60 }61 for i, t := range tp {62 if t == 1 {63 Fprint(out, p-a[i], " ")64 } else {65 Fprint(out, p+a[i], " ")66 }67 }68 Fprintln(out)69 Fprintln(out, p, p+k)70 return true71 }72 73 for _, v := range b {74 if f(a[0]+v) || f(abs(a[0]-v)) {75 continue o76 }77 }78 Fprintln(out, "NO")79 }80}81 8283