- 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
- 101 lines of Go from the credited upstream file 1205B.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 . "fmt"6 "io"7)8 910func Sol1205B(reader io.Reader, writer io.Writer) {11 min := func(a, b int) int {12 if a <= b {13 return a14 }15 return b16 }17 const inf int = 1e818 19 in := bufio.NewReader(reader)20 out := bufio.NewWriter(writer)21 defer out.Flush()22 23 var n int24 Fscan(in, &n)25 arr := make([]int64, n)26 bitIndexes := make([][]int, 60)27 for i := 0; i < n; i++ {28 var a int6429 Fscan(in, &a)30 arr[i] = a31 for j := 0; a > 0; j++ {32 if a&1 == 1 {33 bitIndexes[j] = append(bitIndexes[j], i)34 }35 a >>= 136 }37 }38 39 uniqueSet := map[int64]int{}40 for _, indexes := range bitIndexes {41 if len(indexes) >= 3 {42 Fprintln(out, 3)43 return44 }45 for _, idx := range indexes {46 uniqueSet[arr[idx]] = 147 }48 }49 uniqueArr := []int64{}50 for k := range uniqueSet {51 uniqueArr = append(uniqueArr, k)52 }53 54 55 sz := len(uniqueArr)56 weights := make([][]int, sz)57 for i := range weights {58 weights[i] = make([]int, sz)59 for j := range weights[i] {60 weights[i][j] = inf61 }62 }63 for i := range weights {64 weights[i][i] = 065 }66 for i, ai := range uniqueArr {67 for j := i + 1; j < sz; j++ {68 if ai&uniqueArr[j] > 0 {69 weights[i][j] = 170 weights[j][i] = 171 }72 }73 }74 dist := make([][]int, sz)75 for i := range dist {76 dist[i] = make([]int, sz)77 copy(dist[i], weights[i])78 }79 ans := inf80 for k := 0; k < sz; k++ {81 for i := 0; i < k; i++ {82 for j := 0; j < i; j++ {83 ans = min(ans, dist[i][j]+weights[i][k]+weights[k][j])84 }85 }86 for i := 0; i < sz; i++ {87 for j := 0; j < sz; j++ {88 dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j])89 }90 }91 }92 if ans == inf {93 ans = -194 }95 Fprint(out, ans)96}97 9899100101