Problem solution · Go

Codeforces 1716C — Robot in a Hallway

Codeforces 1716C — Robot in a Hallway: 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
60 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 1716C — Robot in a Hallway, 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

  • 60 lines of Go from the credited upstream file 1716C.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 1716C — Robot in a Hallway · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") // github.com/EndlessCheng/codeforces-gofunc CF1716C(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush()	min := func(a, b int) int {		if a > b {			return b		}		return a	}	max := func(a, b int) int {		if b > a {			return b		}		return a	} 	var T, m int	for Fscan(in, &T); T > 0; T-- {		Fscan(in, &m)		a := make([][2]int, m)		for j := 0; j < 2; j++ {			for i := 0; i < m; i++ {				Fscan(in, &a[i][j])			}		}		// 我的评价是好想不好写的题		// 为了方便描述,下面的坐标以 (0,0) 为左上角		// 小技巧:把起点当成 (0,-1),这样的话需要把 a[0][0] 置为 -1 从而与原问题保持一致		a[0][0] = -1		// suf[i][0] 表示能够「一刻不停地」从 (0,i) 走个反 C 型到 (1,i),在进入 (0,i) 前的最小时间		// suf[i][1] 表示能够「一刻不停地」从 (1,i) 走个反 C 型到 (0,i),在进入 (1,i) 前的最小时间		suf := make([][2]int, m+1)		for i := m - 1; i >= 0; i-- {			suf[i][0] = max(suf[i+1][0]-1, max(a[i][0], a[i][1]-(m-i)*2+1))			suf[i][1] = max(suf[i+1][1]-1, max(a[i][1], a[i][0]-(m-i)*2+1))		}		// i 列及其前的格子走蛇形,i+1 列及其后的格子走反 C 型		// 模拟即可		ans, t := suf[0][0]+m*2, -1		for i, col := range a[:m-1] {			t = max(t, col[i&1]) + 1			t = max(t, col[i&1^1]) + 1			ans = min(ans, max(t, suf[i+1][i&1^1])+(m-1-i)*2)		}		Fprintln(out, ans)	}} //func main() { CF1716C(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 ↗