Approach
Sorting and greedy selection
For Codeforces 1418E — Expected Damage, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 56 lines of Go from the credited upstream file 1418E.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
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 "slices"8 "sort"9)10 1112func cf1418E(in io.Reader, _w io.Writer) {13 out := bufio.NewWriter(_w)14 defer out.Flush()15 const mod = 99824435316 pow := func(x, n int) (res int) {17 res = 118 for ; n > 0; n >>= 1 {19 if n&1 == 1 {20 res = res * x % mod21 }22 x = x * x % mod23 }24 return res25 }26 div := func(a, b int) int { return a * pow(b, mod-2) % mod }27 28 var n, q, def, dur int29 Fscan(in, &n, &q)30 a := make([]int, n)31 for i := range a {32 Fscan(in, &a[i])33 }34 slices.Sort(a)35 sum := make([]int, n+1)36 for i, v := range a {37 sum[i+1] = (sum[i] + v) % mod38 }39 for ; q > 0; q-- {40 Fscan(in, &dur, &def)41 p := sort.SearchInts(a, def)42 cnt := n - p43 if cnt < dur {44 Fprintln(out, 0)45 continue46 }47 ans := div(cnt+1-dur, cnt+1) * sum[p]48 if cnt > dur {49 ans += div(cnt-dur, cnt) * (sum[n] - sum[p] + mod)50 }51 Fprintln(out, ans%mod)52 }53}54 5556