bor

QB

bor
May 13th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.90 KB | None | 0 0
  1. module Main
  2.  
  3. open System
  4.  
  5. #if DEBUG
  6. open System.IO
  7. Console.SetIn(new StreamReader("..\\..\\in.txt"))
  8. Console.SetOut(new StreamWriter("..\\..\\out.txt"))
  9. #endif
  10.  
  11. let readInt () =
  12.   let rec parseInput prev sum neg =
  13.     match char (System.Console.Read()) with
  14.     | '-' -> parseInput prev sum true
  15.     | c when System.Char.IsDigit c -> parseInput c (sum * 10 + int c - int '0') neg
  16.     | c when System.Char.IsDigit prev -> if neg then -sum else sum
  17.     | c -> parseInput c sum neg
  18.   parseInput ' ' 0 false
  19.  
  20. // stuff cutline
  21.  
  22. [<NoEquality>][<NoComparison>]
  23. type Train = { From : int; Length : int; Velocity : int }
  24.  
  25. let solve() =
  26.   let rec test() =
  27.     let n, L, S, V = readInt(), readInt(), readInt(), readInt()
  28.     if n <> 0 then
  29.       let cars = Array.init n (fun _ -> readInt(), readInt(), readInt())
  30.       let track = Array.fold (fun sum (x, l, v) -> sum + l + L) S cars |> float
  31.       let stops =
  32.         let rec folder (t : Train) (stops : Train list) =
  33.           match stops with
  34.           | hd :: tl when hd.Velocity >= t.Velocity -> folder { t with Length = t.Length + hd.Length } tl
  35.           | st -> t :: st
  36.         Array.foldBack (fun (x, l, v) stops -> folder { From = x - l - L; Length = l + L; Velocity = v } stops) cars []
  37.         |> folder { From = 0; Length = 0; Velocity = V }
  38.       let answer =
  39.         let folder (time : float, vel : int, head : float) (train : Train) =
  40.           let colAfter = (float train.From - head) / float (vel - train.Velocity)
  41.           let colPos = head + colAfter * float vel
  42.           if colPos > track then (track - head) / float vel, vel, track
  43.           else time + colAfter, train.Velocity, colPos + float train.Length
  44.         let time, vel, pos = List.fold folder (0.0, V + 1, 0.0) stops
  45.         if pos < track then time + (track - pos) / float vel else time
  46.       printfn "%.10f" answer
  47.       test()
  48.   test()
  49.  
  50. solve()
  51. Console.Out.Flush()
Advertisement
Add Comment
Please, Sign In to add comment