Approach
Sorting and greedy selection
For Codeforces 1221E — Game With String, 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
- 70 lines of Go from the credited upstream file 1221E.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)9 10func cf1221E(in io.Reader, _w io.Writer) {11 out := bufio.NewWriter(_w)12 defer out.Flush()13 in1 := func(x, a, b int) bool { return a <= x && x < 2*b }14 in2 := func(x, a, b int) bool { return x >= 2*b || b <= x && x < a }15 f := func(a, b int, s string) bool {16 n := len(s)17 q := []int{}18 for i := 0; i < n; i++ {19 if s[i] == '.' {20 j := i21 for j+1 < n && s[j+1] == '.' {22 j++23 }24 q = append(q, j-i+1)25 i = j26 }27 }28 if len(q) == 0 {29 return false30 }31 32 slices.Sort(q)33 x, ok := 0, false34 for i := range len(q) - 1 {35 if in1(q[i], a, b) {36 x ^= 137 }38 if in2(q[i], a, b) {39 ok = true40 }41 }42 for i, j := 0, q[len(q)-1]-a; j >= 0; i, j = i+1, j-1 {43 if !ok && !in2(i, a, b) && !in2(j, a, b) && x^b2i21(in1(i, a, b))^b2i21(in1(j, a, b)) == 0 {44 return true45 }46 }47 return false48 }49 50 var T, a, b int51 var s string52 for Fscan(in, &T); T > 0; T-- {53 Fscan(in, &a, &b, &s)54 if f(a, b, s) {55 Fprintln(out, "YES")56 } else {57 Fprintln(out, "NO")58 }59 }60}61 6263 64func b2i21(b bool) int {65 if b {66 return 167 }68 return 069}70