Approach
Direct simulation
For Codeforces 1808D — Petya, Petya, Petr, and Palindromes, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.
- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 45 lines of Go from the credited upstream file 1808D.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
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 CF1808D(_r io.Reader, out io.Writer) {12 in := bufio.NewReader(_r)13 min := func(a, b int) int {14 if a > b {15 return b16 }17 return a18 }19 max := func(a, b int) int {20 if b > a {21 return b22 }23 return a24 }25 26 var n, k int27 Fscan(in, &n, &k)28 a := make([]int, n)29 pos := [2e5 + 1][2][]int{}30 for i := range a {31 Fscan(in, &a[i])32 pos[a[i]][i%2] = append(pos[a[i]][i%2], i)33 }34 ans := int64(0)35 for i, v := range a[:n-1-k/2] {36 ps := pos[v][i%2]37 l := max(i-k/2+1, 0)38 r := min(i+k-1, n-1)39 ans += int64(r - l - k + 2 - sort.SearchInts(ps, (r*2-k+1-i)+1) + sort.SearchInts(ps, l*2+k-1-i))40 }41 Fprint(out, ans)42}43 4445