Approach
Sorting and greedy selection
For Codeforces 1427D — Unshuffling a Deck, 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
- 49 lines of Go from the credited upstream file 1427D.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 . "fmt"5 "io"6 "sort"7)8 910func CF1427D(in io.Reader, out io.Writer) {11 var n int12 Fscan(in, &n)13 a := make([]int, n)14 for i := range a {15 Fscan(in, &a[i])16 }17 ans := [][]interface{}{}18 p := make([]int, n+2)19 for !sort.IntsAreSorted(a) {20 for i, v := range a {21 p[v] = i22 }23 v := 124 for ; p[v] < p[v+1]; v++ {25 }26 w := v + 127 for ; p[w]+1 == p[w+1]; w++ {28 }29 i, j, k := p[v+1], p[w]+1, p[v]+130 sz := []interface{}{}31 if i > 0 {32 sz = append(sz, i)33 }34 sz = append(sz, j-i, k-j)35 if k < n {36 sz = append(sz, n-k)37 }38 ans = append(ans, sz)39 a = append(append(append(a[k:], a[j:k]...), a[i:j]...), a[:i]...)40 }41 Fprintln(out, len(ans))42 for _, sz := range ans {43 Fprint(out, len(sz), " ")44 Fprintln(out, sz...)45 }46}47 4849