Approach
Sorting and greedy selection
For Codeforces 2046C — Adventurers, 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
- 222 lines of Go from the credited upstream file 2046C.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 "cmp"5 . "fmt"6 "io"7 "slices"8 "sort"9 "time"10)11 1213type node46[K comparable] struct {14 son [2]*node46[K]15 priority uint16 key K17 keyCnt int18 subSize int19}20 21func (o *node46[K]) size() int {22 if o != nil {23 return o.subSize24 }25 return 026}27 28func (o *node46[K]) maintain() {29 o.subSize = o.keyCnt + o.son[0].size() + o.son[1].size()30}31 32func (o *node46[K]) rotate(d int) *node46[K] {33 x := o.son[d^1]34 o.son[d^1] = x.son[d]35 x.son[d] = o36 o.maintain()37 x.maintain()38 return x39}40 41type treap46[K comparable] struct {42 rd uint43 root *node46[K]44 comparator func(a, b K) int45}46 47func (t *treap46[K]) fastRand() uint {48 t.rd ^= t.rd << 1349 t.rd ^= t.rd >> 1750 t.rd ^= t.rd << 551 return t.rd52}53 54func (t *treap46[K]) size() int { return t.root.size() }55func (t *treap46[K]) empty() bool { return t.size() == 0 }56 57func (t *treap46[K]) _put(o *node46[K], key K) *node46[K] {58 if o == nil {59 o = &node46[K]{priority: t.fastRand(), key: key, keyCnt: 1}60 } else {61 c := t.comparator(key, o.key)62 if c == 0 {63 o.keyCnt++64 } else {65 d := 066 if c > 0 {67 d = 168 }69 o.son[d] = t._put(o.son[d], key)70 if o.son[d].priority > o.priority {71 o = o.rotate(d ^ 1)72 }73 }74 }75 o.maintain()76 return o77}78 79func (t *treap46[K]) put(key K) { t.root = t._put(t.root, key) }80 81func (t *treap46[K]) _delete(o *node46[K], key K) *node46[K] {82 if o == nil {83 return nil84 }85 if c := t.comparator(key, o.key); c != 0 {86 d := 087 if c > 0 {88 d = 189 }90 o.son[d] = t._delete(o.son[d], key)91 } else {92 if o.keyCnt > 1 {93 o.keyCnt--94 } else {95 if o.son[1] == nil {96 return o.son[0]97 }98 if o.son[0] == nil {99 return o.son[1]100 }101 d := 0102 if o.son[0].priority > o.son[1].priority {103 d = 1104 }105 o = o.rotate(d)106 o.son[d] = t._delete(o.son[d], key)107 }108 }109 o.maintain()110 return o111}112 113func (t *treap46[K]) delete(key K) { t.root = t._delete(t.root, key) }114 115func (t *treap46[K]) lowerBoundIndex(key K) (kth int) {116 for o := t.root; o != nil; {117 c := t.comparator(key, o.key)118 if c < 0 {119 o = o.son[0]120 } else if c > 0 {121 kth += o.son[0].size() + o.keyCnt122 o = o.son[1]123 } else {124 kth += o.son[0].size()125 break126 }127 }128 return129}130 131func (t *treap46[K]) at(k int) (o *node46[K]) {132 if k < 0 || k >= t.root.size() {133 return134 }135 for o = t.root; o != nil; {136 leftSize := o.son[0].size()137 if k < leftSize {138 o = o.son[0]139 } else {140 k -= leftSize + o.keyCnt141 if k < 0 {142 break143 }144 o = o.son[1]145 }146 }147 return148}149 150func newTreap46[K cmp.Ordered]() *treap46[K] {151 return &treap46[K]{152 rd: uint(time.Now().UnixNano())/2 + 1,153 comparator: cmp.Compare[K],154 }155}156 157func cf2046C(in io.Reader, out io.Writer) {158 var T, n int159 for Fscan(in, &T); T > 0; T-- {160 Fscan(in, &n)161 ys := map[int][]int{}162 for range n {163 var x, y int164 Fscan(in, &x, &y)165 ys[x] = append(ys[x], y)166 }167 168 suf := newTreap46[int]()169 xs := make([]int, 0, len(ys))170 for x, ys := range ys {171 xs = append(xs, x)172 for _, y := range ys {173 suf.put(y)174 }175 }176 slices.Sort(xs)177 178 ans, xx, yy := 0, int(-1e9), int(-1e9)179 pre := newTreap46[int]()180 for _, x := range xs {181 if !pre.empty() {182 sort.Search(n/4, func(low int) bool {183 low++184 o := pre.at(low - 1)185 if o == nil {186 return true187 }188 minY := o.key189 o = suf.at(low - 1)190 if o == nil {191 return true192 }193 minY = max(minY, o.key) + 1194 195 i := pre.lowerBoundIndex(minY)196 if pre.size()-i < low {197 return true198 }199 i = suf.lowerBoundIndex(minY)200 if suf.size()-i < low {201 return true202 }203 204 if low > ans {205 ans = low206 xx, yy = x, minY207 }208 return false209 })210 }211 for _, y := range ys[x] {212 pre.put(y)213 suf.delete(y)214 }215 }216 Fprintln(out, ans)217 Fprintln(out, xx, yy)218 }219}220 221222