- 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
- 62 lines of Go from the credited upstream file 1183G.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 hp1183 struct{ sort.IntSlice }12 13func (h hp1183) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }14func (h *hp1183) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }15func (h *hp1183) Pop() (v interface{}) {16 n := len(h.IntSlice)17 h.IntSlice, v = h.IntSlice[:n-1], h.IntSlice[n-1]18 return19}20 2122func CF1183G(_r io.Reader, _w io.Writer) {23 in := bufio.NewReader(_r)24 out := bufio.NewWriter(_w)25 defer out.Flush()26 min := func(a, b int) int {27 if a < b {28 return a29 }30 return b31 }32 33 var t, n, tp, f int34 for Fscan(in, &t); t > 0; t-- {35 Fscan(in, &n)36 types := make([][2]int, n+1)37 for i := 0; i < n; i++ {38 Fscan(in, &tp, &f)39 types[tp][f]++40 }41 cnts := make([][]int, n+1)42 for _, t := range types {43 s := t[0] + t[1]44 cnts[s] = append(cnts[s], t[1])45 }46 h := &hp1183{}47 sum, sumF1 := 0, 048 for i := n; i > 0; i-- {49 for _, f1 := range cnts[i] {50 Push(h, f1)51 }52 if len(h.IntSlice) > 0 {53 sum += i54 sumF1 += min(Pop(h).(int), i)55 }56 }57 Fprintln(out, sum, sumF1)58 }59}60 6162