Problem solution · Go

Codeforces 1359F — RC Kaboom Show

Codeforces 1359F — RC Kaboom Show: 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
83 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 1359F — RC Kaboom Show, 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

  • 83 lines of Go from the credited upstream file 1359F.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 1359F — RC Kaboom Show · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"math") type vec1359 struct{ x, y int } func (a vec1359) sub(b vec1359) vec1359 { return vec1359{a.x - b.x, a.y - b.y} }func (a vec1359) dot(b vec1359) int     { return a.x*b.x + a.y*b.y }func (a vec1359) det(b vec1359) int     { return a.x*b.y - a.y*b.x }func (a vec1359) len() float64          { return math.Hypot(float64(a.x), float64(a.y)) } // github.com/EndlessCheng/codeforces-gofunc CF1359F(_r io.Reader, out io.Writer) {	// faster than math.Min and math.Max	min := func(a, b float64) float64 {		if a < b {			return a		}		return b	}	max := func(a, b float64) float64 {		if a > b {			return a		}		return b	}	type car struct {		p, d    vec1359		s, norm float64	} 	in := bufio.NewReader(_r)	var n int	Fscan(in, &n)	cs := make([]car, n)	for i := range cs {		Fscan(in, &cs[i].p.x, &cs[i].p.y, &cs[i].d.x, &cs[i].d.y, &cs[i].s)		cs[i].norm = cs[i].d.len()	} 	ans := 1e99	for i, p := range cs {		for j := i + 1; j < n; j++ {			q := cs[j]			u := p.p.sub(q.p)			if d := p.d.det(q.d); d != 0 {				d1, d2 := q.d.det(u), p.d.det(u)				if d > 0 && d1 >= 0 && d2 >= 0 || d < 0 && d1 <= 0 && d2 <= 0 {					ans = min(ans, max(p.norm*float64(d1)/(p.s*float64(d)), q.norm*float64(d2)/(q.s*float64(d))))				}				continue			}			if u.det(p.d) != 0 {				continue			}			// u.len() could be avoided https://codeforces.com/contest/1359/submission/81868329			if l := u.len(); p.d.dot(q.d) > 0 {				if u.dot(q.d) >= 0 {					ans = min(ans, l/q.s)				} else {					ans = min(ans, l/p.s)				}			} else {				if u.dot(q.d) > 0 {					ans = min(ans, l/(p.s+q.s))				}			}		}	}	if ans == 1e99 {		Fprint(out, "No show :(")	} else {		Fprintf(out, "%.8f", ans)	}} //func main() { CF1359F(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 ↗