Problem solution · Go

Codeforces 802N

Codeforces 802N: 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
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

Direct simulation

For Codeforces 802N, 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

  • 90 lines of Go from the credited upstream file 802N.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.

Source

Code and credit

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

Full codeCodeforces 802N · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io") // https://github.com/EndlessChengfunc cf802N(in io.Reader, out io.Writer) {	const inf int = 1e18	var n, k, v int	Fscan(in, &n, &k) 	st, st2, end := n, n+1, n+2	type nb struct{ to, rid, cap, cost int }	g := make([][]nb, end+1)	addEdge := func(from, to, cap, cost int) {		g[from] = append(g[from], nb{to, len(g[to]), cap, cost})		g[to] = append(g[to], nb{from, len(g[from]) - 1, 0, -cost})	} 	addEdge(st, st2, k, 0)	for i := range n {		Fscan(in, &v)		addEdge(st2, i, 1, v)		if i > 0 {			addEdge(i-1, i, inf, 0) // 让时间流动!		}	}	for i := range n {		Fscan(in, &v)		addEdge(i, end, 1, v)	} 	dis := make([]int, len(g))	type vi struct{ v, i int }	fa := make([]vi, len(g))	inQ := make([]bool, len(g))	spfa := func() bool {		for i := range dis {			dis[i] = inf		}		dis[st] = 0		inQ[st] = true		q := []int{st}		for len(q) > 0 {			v := q[0]			q = q[1:]			inQ[v] = false			for i, e := range g[v] {				if e.cap == 0 {					continue				}				w := e.to				newD := dis[v] + e.cost				if newD < dis[w] {					dis[w] = newD					fa[w] = vi{v, i}					if !inQ[w] {						inQ[w] = true						q = append(q, w)					}				}			}		}		return dis[end] < inf	} 	minCost := 0	for spfa() {		minF := inf		for v := end; v != st; {			p := fa[v]			minF = min(minF, g[p.v][p.i].cap)			v = p.v		}		for v := end; v != st; {			p := fa[v]			e := &g[p.v][p.i]			e.cap -= minF			g[v][e.rid].cap += minF			v = p.v		}		minCost += dis[end] * minF	}	Fprint(out, minCost)} //func main() { cf802N(bufio.NewReader(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 ↗