Approach
Sorting and greedy selection
For Codeforces 1207D — Number Of Permutations, 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
- 42 lines of Go from the credited upstream file 1207D.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 "sort"8)9 1011func CF1207D(_r io.Reader, out io.Writer) {12 in := bufio.NewReader(_r)13 const mod = 99824435314 var n, x, y int15 var ans, sa, sb, sp int64 = 1, 1, 1, 116 Fscan(in, &n)17 type pair struct{ x, y int }18 a := make([]pair, n)19 ca := make([]int, n+1)20 cb := make([]int, n+1)21 cp := make(map[pair]int, n)22 for i := range a {23 Fscan(in, &x, &y)24 a[i] = pair{x, y}25 ca[x]++26 cb[y]++27 cp[a[i]]++28 ans = ans * int64(i+1) % mod29 sa = sa * int64(ca[x]) % mod30 sb = sb * int64(cb[y]) % mod31 sp = sp * int64(cp[a[i]]) % mod32 }33 ans -= sa + sb34 sort.Slice(a, func(i, j int) bool { a, b := a[i], a[j]; return a.x < b.x || a.x == b.x && a.y < b.y })35 if sort.SliceIsSorted(a, func(i, j int) bool { return a[i].y < a[j].y }) {36 ans += sp37 }38 Fprint(out, (ans%mod+mod)%mod)39}40 4142