Problem solution · Go

Codeforces 1340C — Nastya and Unexpected Guest

Codeforces 1340C — Nastya and Unexpected Guest: a Go solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Direct simulation
Source
EndlessCheng Codeforces Go
Length
106 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Codeforces 1340C — Nastya and Unexpected Guest, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 106 lines of Go from the credited upstream file 1340C.go.
  • The implementation visibly relies on sequence storage, work queue.
  • 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.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 1340C — Nastya and Unexpected Guest · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"sort") // github.com/EndlessCheng/codeforces-gotype pair40 struct{ i, t int }type deque40 struct{ l, r []pair40 } func (q deque40) empty() bool     { return len(q.l) == 0 && len(q.r) == 0 }func (q *deque40) pushL(v pair40) { q.l = append(q.l, v) }func (q *deque40) pushR(v pair40) { q.r = append(q.r, v) }func (q *deque40) popL() (v pair40) {	if len(q.l) > 0 {		q.l, v = q.l[:len(q.l)-1], q.l[len(q.l)-1]	} else {		v, q.r = q.r[0], q.r[1:]	}	return} func CF1340C(_r io.Reader, out io.Writer) {	in := bufio.NewReader(_r)	min := func(a, b int64) int64 {		if a > b {			return b		}		return a	} 	var n, m, g, r int	Fscan(in, &n, &m)	x := make([]int, m)	for i := range x {		Fscan(in, &x[i])	}	sort.Ints(x)	Fscan(in, &g, &r)	for i := 1; i < m; i++ {		if x[i]-x[i-1] > g {			Fprint(out, -1)			return		}	} 	dis := make([][]int, m)	for i := range dis {		dis[i] = make([]int, g+1)		for j := range dis[i] {			dis[i][j] = 1e9		}	}	dis[0][g] = 0	q := &deque40{}	q.pushL(pair40{0, g})	for !q.empty() {		p := q.popL()		i, t := p.i, p.t		d := dis[i][t]		if t > 0 {			if i > 0 {				if j := t - (x[i] - x[i-1]); j >= 0 && d < dis[i-1][j] {					dis[i-1][j] = d					q.pushL(pair40{i - 1, j})				}			}			if i < m-1 {				if j := t - (x[i+1] - x[i]); j >= 0 && d < dis[i+1][j] {					dis[i+1][j] = d					q.pushL(pair40{i + 1, j})				}			}		} else {			newD := d + 1			if i > 0 {				if j := g - (x[i] - x[i-1]); newD < dis[i-1][j] {					dis[i-1][j] = newD					q.pushR(pair40{i - 1, j})				}			}			if i < m-1 {				if j := g - (x[i+1] - x[i]); newD < dis[i+1][j] {					dis[i+1][j] = newD					q.pushR(pair40{i + 1, j})				}			}		}	}	ans := int64(1e18)	for j, d := range dis[m-1] {		if d < 1e9 {			ans = min(ans, int64(d)*int64(r+g)+int64(g-j))		}	}	if ans == 1e18 {		ans = -1	}	Fprint(out, ans)} //func main() { CF1340C(os.Stdin, os.Stdout) } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗