Approach
Sorting and greedy selection
For Codeforces 1621E — New School, 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
- 98 lines of Go from the credited upstream file 1621E.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 "bytes"6 . "fmt"7 "io"8 "sort"9)10 1112func CF1621E(_r io.Reader, _w io.Writer) {13 in := bufio.NewReader(_r)14 out := bufio.NewWriter(_w)15 defer out.Flush()16 type group struct {17 a []int18 s int6419 i int20 }21 22 var T, n, m, k int23 for Fscan(in, &T); T > 0; T-- {24 Fscan(in, &n, &m)25 te := make(sort.IntSlice, n)26 for i := range te {27 Fscan(in, &te[i])28 }29 sort.Sort(sort.Reverse(te))30 te = te[:m]31 gs := make([]group, m)32 for i := range gs {33 Fscan(in, &k)34 a := make([]int, k)35 for j := range a {36 Fscan(in, &a[j])37 gs[i].s += int64(a[j])38 }39 gs[i].a = a40 gs[i].i = i41 }42 sort.Slice(gs, func(i, j int) bool { a, b := gs[i], gs[j]; return a.s*int64(len(b.a)) > b.s*int64(len(a.a)) })43 44 match := make([]int, m+1)45 matchL := make([]int, m+1)46 matchR := make([]int, m+1)47 for i, v := range te {48 match[i+1] = match[i]49 if int64(v)*int64(len(gs[i].a)) >= gs[i].s {50 match[i+1]++51 }52 matchL[i+1] = matchL[i]53 if i > 0 && int64(v)*int64(len(gs[i-1].a)) >= gs[i-1].s {54 matchL[i+1]++55 }56 matchR[i+1] = matchR[i]57 if i < m-1 && int64(v)*int64(len(gs[i+1].a)) >= gs[i+1].s {58 matchR[i+1]++59 }60 }61 62 ans := make([][]byte, m)63 for i, g := range gs {64 res := bytes.Repeat([]byte{'0'}, len(g.a))65 for ri, v := range g.a {66 v := int64(v)67 l := int64(len(g.a))68 if v*l == g.s {69 if match[m] == m {70 res[ri] = '1'71 }72 continue73 }74 j := sort.Search(len(te), func(j int) bool { return int64(te[j])*(l-1) < g.s-v }) - 175 if j < 0 {76 continue77 }78 if v*l < g.s { 79 if matchL[i+1]-matchL[j+1] == i-j && match[m]-(match[i+1]-match[j]) == m-(i+1-j) {80 res[ri] = '1'81 }82 } else { 83 if matchR[j]-matchR[i] == j-i && match[m]-(match[j+1]-match[i]) == m-(j+1-i) {84 res[ri] = '1'85 }86 }87 }88 ans[g.i] = res89 }90 for _, s := range ans {91 Fprintf(out, "%s", s)92 }93 Fprintln(out)94 }95}96 9798