- 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
- 119 lines of Go from the credited upstream file 1826E.go.
- The implementation visibly relies on sequence storage, ordered lookup.
- 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 . "fmt"5 "io"6 "math"7 "math/bits"8 "sort"9)10 1112const _w26 = bits.UintSize13 14func NewBitset26(n int) Bitset26 { return make(Bitset26, n/_w26+1) } 15 16type Bitset26 []uint17 18func (b Bitset26) Has(p int) bool { return b[p/_w26]&(1<<(p%_w26)) != 0 } 19func (b Bitset26) Set(p int) { b[p/_w26] |= 1 << (p % _w26) } 20func (b Bitset26) SetAll1() {21 for i := range b {22 b[i] = math.MaxUint23 }24}25func (b Bitset26) IntersectionFrom(c Bitset26) {26 for i, v := range c {27 b[i] &= v28 }29}30 31func CF1826E(_r io.Reader, out io.Writer) {32 _i, _n, buf := 0, 0, make([]byte, 1<<12)33 rc := func() byte {34 if _i == _n {35 _n, _ = _r.Read(buf)36 if _n == 0 {37 return 038 }39 _i = 040 }41 b := buf[_i]42 _i++43 return b44 }45 ri := func() (x int) {46 b := rc()47 for ; '0' > b; b = rc() {48 }49 for ; '0' <= b; b = rc() {50 x = x*10 + int(b&15)51 }52 return53 }54 max := func(a, b int64) int64 {55 if b > a {56 return b57 }58 return a59 }60 61 m, n := ri(), ri()62 type pair struct {63 p int64 r []int65 }66 a := make([]pair, n)67 for i := range a {68 a[i].p = ri()69 a[i].r = make([]int, m)70 }71 72 for i := 0; i < m; i++ {73 for _, p := range a {74 p.r[i] = ri()75 }76 }77 78 sort.Slice(a, func(i, j int) bool { return a[i].r[0] < a[j].r[0] })79 80 from := make([]Bitset26, n)81 for i := range from {82 from[i] = NewBitset26(n)83 from[i].SetAll1()84 }85 86 ids := make([]int, n)87 for i := range ids {88 ids[i] = i89 }90 for city := 0; city < m; city++ {91 sort.Slice(ids, func(i, j int) bool { return a[ids[i]].r[city] < a[ids[j]].r[city] })92 cur := NewBitset26(n)93 j := 094 for _, i := range ids {95 for a[ids[j]].r[city] < a[i].r[city] {96 cur.Set(ids[j])97 j++98 }99 from[i].IntersectionFrom(cur)100 }101 }102 103 ans := int64(0)104 f := make([]int64, n)105 for i, p := range a {106 f[i] = 0107 for j := i - 1; j >= 0; j-- {108 if f[j] > f[i] && from[i].Has(j) {109 f[i] = f[j]110 }111 }112 f[i] += int64(p.p)113 ans = max(ans, f[i])114 }115 Fprint(out, ans)116}117 118119