- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 85 lines of Go from the credited upstream file 372C.go.
- The implementation visibly relies on sequence storage, cached states.
- No explicit loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
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)8 910type data72 struct {11 v int6412 del int13}14type mq72 struct {15 data []data7216 size int17}18 19func (q mq72) less(a, b data72) bool { return a.v >= b.v }20func (q *mq72) push(v int64) {21 q.size++22 d := data72{v, 1}23 for len(q.data) > 0 && q.less(d, q.data[len(q.data)-1]) {24 d.del += q.data[len(q.data)-1].del25 q.data = q.data[:len(q.data)-1]26 }27 q.data = append(q.data, d)28}29func (q *mq72) pop() {30 q.size--31 if q.data[0].del > 1 {32 q.data[0].del--33 } else {34 q.data = q.data[1:]35 }36}37func (q mq72) top() (v int64) { return q.data[0].v }38 39func CF372C(_r io.Reader, out io.Writer) {40 in := bufio.NewReader(_r)41 abs := func(x int) int {42 if x < 0 {43 return -x44 }45 return x46 }47 48 var n, m, d, pos, hp, t, preT int49 Fscan(in, &n, &m, &d)50 pre := make([]int64, n)51 dp := make([]int64, n)52 for ; m > 0; m-- {53 Fscan(in, &pos, &hp, &t)54 pos--55 q := mq72{}56 half := n57 if int64(d)*int64(t-preT) < int64(n) {58 half = d * (t - preT)59 }60 for _, v := range pre[:half] {61 q.push(v)62 }63 for i := range dp {64 if i+half < n {65 q.push(pre[i+half])66 }67 if i > half {68 q.pop()69 }70 dp[i] = q.top() + int64(hp-abs(pos-i))71 }72 pre, dp = dp, pre73 preT = t74 }75 ans := pre[0]76 for _, v := range pre[1:] {77 if v > ans {78 ans = v79 }80 }81 Fprint(out, ans)82}83 8485