Last active
August 29, 2015 14:12
-
-
Save vkostyukov/afe0e806e18335951b47 to your computer and use it in GitHub Desktop.
CoconutDelivery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val energy = Console.readLine().toInt | |
val streams: List[(Int, Int, Int)] = | |
Iterator.continually(Console.readLine).takeWhile(_.nonEmpty).map { s: String => | |
val stream = s.split(" ") | |
(stream(0).toInt, stream(1).toInt, stream(2).toInt) | |
} toList | |
val (_, end, _) = streams.maxBy { | |
case (_, to, _) => to | |
} | |
def cocount(i: Int, e: Int, s: List[(Int, Int, Int)]): Int = | |
if (i == end) e | |
else s.dropWhile { | |
case (from, _, _) => i > from | |
} match { | |
case (from, to, cost) :: tl => | |
val spend = (from - i) * energy | |
cocount(to, e + spend + cost, tl) min | |
cocount(from, e + spend, tl) | |
case Nil => e + ((end - i) * energy) | |
} | |
println("Answer: " + cocount(0, 0, streams)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment