Skip to content

Instantly share code, notes, and snippets.

@daclouds
Last active August 29, 2015 14:16
Show Gist options
  • Save daclouds/005ac601e96e64eb88cc to your computer and use it in GitHub Desktop.
Save daclouds/005ac601e96e64eb88cc to your computer and use it in GitHub Desktop.
import java.io.{BufferedReader, FileReader}
object KingdomRush extends App {
val input = new BufferedReader(new FileReader("B-small-practice.in"))
val T: Int = input.readLine().toInt
def minTimes(list:List[Array[Int]], stars:Int, total:Int):Int = {
if(list.length == 0) return total
val (sat, others) = list.partition(x => x(1) <= stars )
if(sat.length == 0) {
val index = list.indexWhere(x => x(0) <= stars)
if(index == -1 ) return -1
else {
val xs = list.updated(index, Array(-1, list(index)(1)))
minTimes(xs, stars +1, total + 1)
}
} else {
val earnedStars = sat.flatten.filter(_ > -1).length
minTimes(others, stars + earnedStars, sat.length + total)
}
}
for (t <- 0 until T) {
val stage = input.readLine().toInt
val list = for {
s <- (0 until stage).toList
} yield {
input.readLine().split(" ").map(_.toInt)
}
val result = minTimes(list, 0, 0)
print(f"Case #${t + 1}: ")
if (result < 0) {
println("Too Bad")
} else println(result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment