-
-
Save theodoreLee/7255870785c4664d6112 to your computer and use it in GitHub Desktop.
This file contains 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
import java.io.FileOutputStream | |
import scala.io.Source | |
object PickingUpChicks { | |
val INPUT = "B-large-practice.in" | |
val OUTPUT = INPUT.takeWhile(_ != '.') + ".out2" | |
def main(args: Array[String]): Unit = { | |
val itr = Source.fromFile(INPUT).getLines() | |
val sets = itr.next().toInt | |
val writer = new FileOutputStream(OUTPUT) | |
// val writer = Console.out | |
try { | |
Console.withOut(writer) { | |
for (set <- 1 to sets) { | |
val Array(n,k,b,t) = itr.next().split(' ').map(_.toInt) | |
val x = itr.next().split(' ').map(_.toInt) | |
val v = itr.next().split(' ').map(_.toInt) | |
println(f"Case #${set}: ${solve(n,k,b,t,x,v)}") | |
} | |
} | |
} finally { | |
writer.close() | |
} | |
} | |
def solve(n:Int, k:Int, barn:Int, time:Int, pos:Array[Int], v:Array[Int]) = { | |
def inner(n:Int, k:Int, swapCnt:Int = 0, impossibleChks:Int = 0): Option[Int]= (n,k) match { | |
case (_, 0) => Some(swapCnt) | |
case (0, _) => None | |
case _ if((barn - pos(n-1)) <= v(n-1) * time) => inner(n-1, k-1, swapCnt + impossibleChks, impossibleChks) | |
case _ => inner(n-1, k, swapCnt, impossibleChks + 1) | |
} | |
inner(n,k).getOrElse("IMPOSSIBLE") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment