Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Main
- open System
- #if DEBUG
- open System.IO
- Console.SetIn(new StreamReader("..\\..\\in.txt"))
- Console.SetOut(new StreamWriter("..\\..\\out.txt"))
- #endif
- let readInt () =
- let rec parseInput prev sum neg =
- match char (System.Console.Read()) with
- | '-' -> parseInput prev sum true
- | c when System.Char.IsDigit c -> parseInput c (sum * 10 + int c - int '0') neg
- | c when System.Char.IsDigit prev -> if neg then -sum else sum
- | c -> parseInput c sum neg
- parseInput ' ' 0 false
- // stuff cutline
- [<NoEquality>][<NoComparison>]
- type Train = { From : int; Length : int; Velocity : int }
- let solve() =
- let rec test() =
- let n, L, S, V = readInt(), readInt(), readInt(), readInt()
- if n <> 0 then
- let cars = Array.init n (fun _ -> readInt(), readInt(), readInt())
- let track = Array.fold (fun sum (x, l, v) -> sum + l + L) S cars |> float
- let stops =
- let rec folder (t : Train) (stops : Train list) =
- match stops with
- | hd :: tl when hd.Velocity >= t.Velocity -> folder { t with Length = t.Length + hd.Length } tl
- | st -> t :: st
- Array.foldBack (fun (x, l, v) stops -> folder { From = x - l - L; Length = l + L; Velocity = v } stops) cars []
- |> folder { From = 0; Length = 0; Velocity = V }
- let answer =
- let folder (time : float, vel : int, head : float) (train : Train) =
- let colAfter = (float train.From - head) / float (vel - train.Velocity)
- let colPos = head + colAfter * float vel
- if colPos > track then (track - head) / float vel, vel, track
- else time + colAfter, train.Velocity, colPos + float train.Length
- let time, vel, pos = List.fold folder (0.0, V + 1, 0.0) stops
- if pos < track then time + (track - pos) / float vel else time
- printfn "%.10f" answer
- test()
- test()
- solve()
- Console.Out.Flush()
Advertisement
Add Comment
Please, Sign In to add comment