Problem solution · Go

Codeforces 1422D — Returning Home

Codeforces 1422D — Returning Home: a Go solution using stack-based processing. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Stack-based processing
Source
EndlessCheng Codeforces Go
Length
90 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

Stack-based processing

For Codeforces 1422D — Returning Home, the implementation keeps unresolved items in last-in, first-out order, often to match boundaries, parse structure, or maintain monotonic candidates.

  1. Define what every stack entry represents.
  2. Pop entries once the current item resolves or invalidates them.
  3. Push the current item with only the information later steps need.

Code notes

  • 90 lines of Go from the credited upstream file 1422D.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

If each item is pushed and popped at most once, the stack work is linear.

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 1422D — Returning Home · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	"container/heap"	. "fmt"	"io"	"sort") // github.com/EndlessCheng/codeforces-gotype vd22 struct{ v, d int }type hp22 []vd22 func (h hp22) Len() int              { return len(h) }func (h hp22) Less(i, j int) bool    { return h[i].d < h[j].d }func (h hp22) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }func (h *hp22) Push(v interface{})   { *h = append(*h, v.(vd22)) }func (h *hp22) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }func (h *hp22) push(v vd22)          { heap.Push(h, v) }func (h *hp22) pop() vd22            { return heap.Pop(h).(vd22) } func CF1422D(_r io.Reader, out io.Writer) {	in := bufio.NewReader(_r)	min := func(a, b int) int {		if a < b {			return a		}		return b	}	abs := func(x int) int {		if x < 0 {			return -x		}		return x	} 	var n, sx, sy, tx, ty, x, y int	Fscan(in, &n, &n, &sx, &sy, &tx, &ty)	dist := make([]int, n)	end := make([]int, n)	q := make(hp22, n)	type pt struct{ x, y, i int }	ps := make([]pt, n)	qs := make([]pt, n)	for i := range dist {		Fscan(in, &x, &y)		dist[i] = min(abs(sx-x), abs(sy-y))		end[i] = abs(x-tx) + abs(y-ty)		q[i] = vd22{i, dist[i]}		ps[i] = pt{x, y, i}		qs[i] = pt{y, x, i}	} 	type nb struct{ to, wt int }	g := make([][]nb, n)	for _, ps := range [][]pt{ps, qs} {		sort.Slice(ps, func(i, j int) bool { a, b := ps[i], ps[j]; return a.x < b.x || a.x == b.x && a.y < b.y })		for i := 1; i < n; i++ {			p, q := ps[i-1], ps[i]			v, w, d := p.i, q.i, min(q.x-p.x, abs(q.y-p.y))			g[v] = append(g[v], nb{w, d})			g[w] = append(g[w], nb{v, d})		}	} 	heap.Init(&q)	for len(q) > 0 {		p := q.pop()		v := p.v		if dist[v] < p.d {			continue		}		for _, e := range g[v] {			w := e.to			if newD := dist[v] + e.wt; newD < dist[w] {				dist[w] = newD				q.push(vd22{w, newD})			}		}	}	ans := abs(sx-tx) + abs(sy-ty)	for i, d := range dist {		ans = min(ans, d+end[i])	}	Fprint(out, ans)} //func main() { CF1422D(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 ↗