Approach
Sorting and greedy selection
For Codeforces 1844F2 — Min Cost Permutation (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
- 214 lines of Go from the credited upstream file 1844F2.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 "cmp"6 . "fmt"7 "io"8 "slices"9 "time"10)11 12type nodeS44[K comparable] struct {13 son [2]*nodeS44[K]14 priority uint15 key K16 subSize int17}18 19func (o *nodeS44[K]) size() int {20 if o != nil {21 return o.subSize22 }23 return 024}25 26func (o *nodeS44[K]) maintain() {27 o.subSize = 1 + o.son[0].size() + o.son[1].size()28}29 30func (o *nodeS44[K]) rotate(d int) *nodeS44[K] {31 x := o.son[d^1]32 o.son[d^1] = x.son[d]33 x.son[d] = o34 o.maintain()35 x.maintain()36 return x37}38 39type treapS44[K comparable] struct {40 rd uint41 root *nodeS44[K]42 comparator func(a, b K) int43}44 45func (t *treapS44[K]) fastRand() uint {46 t.rd ^= t.rd << 1347 t.rd ^= t.rd >> 1748 t.rd ^= t.rd << 549 return t.rd50}51 52func (t *treapS44[K]) size() int { return t.root.size() }53func (t *treapS44[K]) empty() bool { return t.size() == 0 }54 55func (t *treapS44[K]) _put(o *nodeS44[K], key K) *nodeS44[K] {56 if o == nil {57 o = &nodeS44[K]{priority: t.fastRand(), key: key}58 } else {59 c := t.comparator(key, o.key)60 if c != 0 {61 d := 062 if c > 0 {63 d = 164 }65 o.son[d] = t._put(o.son[d], key)66 if o.son[d].priority > o.priority {67 o = o.rotate(d ^ 1)68 }69 }70 }71 o.maintain()72 return o73}74 75func (t *treapS44[K]) put(key K) { t.root = t._put(t.root, key) }76 77func (t *treapS44[K]) _delete(o *nodeS44[K], key K) *nodeS44[K] {78 if o == nil {79 return nil80 }81 if c := t.comparator(key, o.key); c != 0 {82 d := 083 if c > 0 {84 d = 185 }86 o.son[d] = t._delete(o.son[d], key)87 } else {88 if o.son[1] == nil {89 return o.son[0]90 }91 if o.son[0] == nil {92 return o.son[1]93 }94 d := 095 if o.son[0].priority > o.son[1].priority {96 d = 197 }98 o = o.rotate(d)99 o.son[d] = t._delete(o.son[d], key)100 }101 o.maintain()102 return o103}104 105func (t *treapS44[K]) delete(key K) { t.root = t._delete(t.root, key) }106 107func (t *treapS44[K]) lowerBoundIndex(key K) (kth int) {108 for o := t.root; o != nil; {109 c := t.comparator(key, o.key)110 if c < 0 {111 o = o.son[0]112 } else if c > 0 {113 kth += o.son[0].size() + 1114 o = o.son[1]115 } else {116 kth += o.son[0].size()117 break118 }119 }120 return121}122 123func (t *treapS44[K]) kth(k int) (o *nodeS44[K]) {124 if k < 0 || k >= t.root.size() {125 return126 }127 for o = t.root; o != nil; {128 leftSize := o.son[0].size()129 if k < leftSize {130 o = o.son[0]131 } else {132 k -= leftSize + 1133 if k < 0 {134 break135 }136 o = o.son[1]137 }138 }139 return140}141 142func newSetWith44[K comparable](comp func(a, b K) int) *treapS44[K] {143 return &treapS44[K]{144 rd: uint(time.Now().UnixNano()),145 comparator: comp,146 }147}148 149func cf1844F2(in io.Reader, _w io.Writer) {150 out := bufio.NewWriter(_w)151 defer out.Flush()152 var T, n, c int153 for Fscan(in, &T); T > 0; T-- {154 Fscan(in, &n, &c)155 a := make([]int, n+2)156 l := make([]int, n+2)157 r := make([]int, n+2)158 for i := 1; i <= n; i++ {159 Fscan(in, &a[i])160 }161 162 var ans []int163 if c >= 0 {164 slices.Sort(a[1:n+1])165 ans = a166 } else {167 slices.SortFunc(a[1:n+1], func(a, b int) int { return b - a })168 ans = make([]int, n+1)169 ans[1] = a[1]170 r[1] = 2171 l[n+1] = n172 for i := 2; i <= n; i++ {173 l[i] = i - 1174 r[i] = i + 1175 }176 177 type pair struct{ x, y int }178 s := newSetWith44[pair](func(a, b pair) int {return cmp.Or(a.x-b.x, a.y-b.y)})179 for i := 3; i < n; i++ {180 if a[r[i]]-a[l[i]] >= c {181 s.put(pair{a[i], i})182 }183 }184 185 for i := 2; i <= n; i++ {186 pos := r[1]187 idx := s.lowerBoundIndex(pair{ans[i-1] + c, 0})188 if idx < s.size() {189 pos = s.kth(idx).key.y190 }191 ans[i] = a[pos]192 s.delete(pair{a[pos], pos})193 x := l[pos]194 y := r[pos]195 r[x] = y196 l[y] = x197 if r[x] > n || a[r[x]]-a[l[x]] < c {198 s.delete(pair{a[x], x})199 }200 if l[y] < 2 || a[r[y]]-a[l[y]] < c {201 s.delete(pair{a[y], y})202 }203 }204 }205 206 for i := 1; i <= n; i++ {207 Fprint(out, ans[i], " ")208 }209 Fprintln(out)210 }211}212 213214