- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 74 lines of Go from the credited upstream file 725D.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
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 . "container/heap"6 . "fmt"7 "io"8 "sort"9)10 11type int64Heap725 []int6412 13func (h int64Heap725) Len() int { return len(h) }14func (h int64Heap725) Less(i, j int) bool { return h[i] < h[j] }15func (h int64Heap725) Swap(i, j int) { h[i], h[j] = h[j], h[i] }16func (h *int64Heap725) Push(v interface{}) { *h = append(*h, v.(int64)) }17func (h *int64Heap725) Pop() (v interface{}) { n := len(*h); *h, v = (*h)[:n-1], (*h)[n-1]; return }18 1920func CF725D(_r io.Reader, _w io.Writer) {21 in := bufio.NewReader(_r)22 out := bufio.NewWriter(_w)23 defer out.Flush()24 type team struct{ balls, weight int64 }25 26 var n, ans int27 var myBalls, w, gives int6428 Fscan(in, &n, &myBalls, &w)29 ts := make([]team, n-1, n)30 for i := range ts {31 Fscan(in, &ts[i].balls, &ts[i].weight)32 }33 sort.Slice(ts, func(i, j int) bool { return ts[i].balls > ts[j].balls })34 ts = append(ts, team{})35 36 for _, t := range ts {37 if b := t.balls; b <= myBalls {38 gives += myBalls - b39 myBalls = b40 break41 }42 ans++43 }44 h := &int64Heap725{}45 for i := 0; ts[i].balls > 0; {46 for ; ts[i].balls > myBalls; i++ {47 Push(h, ts[i].weight-ts[i].balls+1)48 }49 for len(*h) > 0 {50 d := (*h)[0]51 if d > gives {52 break53 }54 Pop(h)55 gives -= d56 }57 if len(*h) < ans {58 ans = len(*h)59 }60 for _, t := range ts[i:] {61 if b := t.balls; b < myBalls {62 gives += myBalls - b63 myBalls = b64 break65 }66 }67 }68 Fprint(out, ans+1)69}70 71727374