Approach
Stack-based processing
For Codeforces 416C — Booking System, the implementation keeps unresolved items in last-in, first-out order, often to match boundaries, parse structure, or maintain monotonic candidates.
- Define what every stack entry represents.
- Pop entries once the current item resolves or invalidates them.
- Push the current item with only the information later steps need.
Code notes
- 59 lines of Go from the credited upstream file 416C.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
If each item is pushed and popped at most once, the stack work is linear.
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 "container/heap"5 . "fmt"6 "io"7 "sort"8)9 1011var a16 [1e3]struct{ v, p, i int }12type hp16 struct{ sort.IntSlice }13func (h hp16) Less(i, j int) bool { return a16[h.IntSlice[i]].p < a16[h.IntSlice[j]].p }14func (h *hp16) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }15func (h *hp16) Pop() interface{} { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }16 17func CF416C(in io.Reader, out io.Writer) {18 var n, m, sum int19 Fscan(in, &n)20 a := a16[:n]21 for i := range a {22 Fscan(in, &a[i].v, &a[i].p)23 a[i].i = i + 124 }25 sort.Slice(a, func(i, j int) bool { return a[i].v > a[j].v })26 Fscan(in, &m)27 type pair struct{ v, i int }28 t := make([]pair, m+1)29 for i := 1; i <= m; i++ {30 Fscan(in, &t[i].v)31 t[i].i = i32 }33 sort.Slice(t, func(i, j int) bool { return t[i].v > t[j].v })34 35 h, j := hp16{}, 036 for i, p := range a {37 for ; t[j].v >= p.v; j++ {38 }39 heap.Push(&h, i)40 if h.Len() > j {41 heap.Pop(&h)42 }43 }44 b := h.IntSlice45 for _, i := range b {46 sum += a[i].p47 }48 Fprintln(out, len(b), sum)49 sort.Slice(b, func(i, j int) bool { return a[b[i]].v > a[b[j]].v })50 j = -151 for _, i := range b {52 for j++; t[j].v < a[i].v; j++ {53 }54 Fprintln(out, a[i].i, t[j].i)55 }56}57 5859