Problem solution · Go

Codeforces 650D — Zip-line

Codeforces 650D — Zip-line: 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
87 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 650D — Zip-line, 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

  • 87 lines of Go from the credited upstream file 650D.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 650D — Zip-line · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"sort") // https://space.bilibili.com/206214func cf650D(in io.Reader, _w io.Writer) {	out := bufio.NewWriter(_w)	defer out.Flush()	var n, m int	Fscan(in, &n, &m)	a := make([]int, n)	for i := range a {		Fscan(in, &a[i])	}	type pair struct{ qid, v int }	qs := make([][]pair, n)	for i := 0; i < m; i++ {		var j, v int		Fscan(in, &j, &v)		qs[j-1] = append(qs[j-1], pair{i, v}) // 离线询问	} 	ans := make([]int, m)	pre := make([]int, n)	g := []int{}	for i, v := range a {		for _, p := range qs[i] {			ans[p.qid] = sort.SearchInts(g, p.v) + 1		}		p := sort.SearchInts(g, v)		if p < len(g) {			g[p] = v		} else {			g = append(g, v)		}		pre[i] = p + 1	} 	suf := make([]int, n)	g = g[:0]	for i := n - 1; i >= 0; i-- {		for _, p := range qs[i] {			// 累加后,我们得到了包含 a[i]=p.v 的 LIS 长度			ans[p.qid] += sort.SearchInts(g, -p.v)		}		v := -a[i]		p := sort.SearchInts(g, v)		if p < len(g) {			g[p] = v		} else {			g = append(g, v)		}		suf[i] = p + 1	} 	lis := len(g)	cnt := make([]int, n+1)	for i, p := range pre {		if p+suf[i]-1 == lis {			cnt[p]++		}	} 	for i, p := range pre {		// k 为不含 a[i] 的 LIS 长度		k := lis		if p+suf[i]-1 == lis && cnt[p] == 1 { // a[i] 在所有 LIS 中			k--		}		for _, p := range qs[i] {			// 两种情况取最大值,即为最终答案			ans[p.qid] = max(ans[p.qid], k)		}	} 	for _, v := range ans {		Fprintln(out, v)	}} //func main() { cf650D(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 ↗