Skip to content

Instantly share code, notes, and snippets.

@theodoreLee
Created April 24, 2015 13:14
Show Gist options
  • Save theodoreLee/29d5718d49bd2e2c20c4 to your computer and use it in GitHub Desktop.
Save theodoreLee/29d5718d49bd2e2c20c4 to your computer and use it in GitHub Desktop.
import java.io.FileOutputStream
import scala.io.Source
/**
* Created by teddy on 2015. 4. 24..
*/
object HouseOfPancakes {
val INPUT = "B-large-practice.in"
val OUTPUT = INPUT.takeWhile(_ != '.') + ".out"
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) {
itr.next()
val sortedList = itr.next().split(' ').map(_.toInt).sortWith(_ > _)
println(s"Case #${set}: ${findOpticalMinutes(sortedList)}")
}
}
} finally {
writer.flush()
writer.close()
}
}
def findOpticalMinutes(list: Array[Int]) = {
val max = list.max
(1 to max).map(eating(list, _, max)).min
}
//다 나눠주고 먹는데 까지 걸린 시간.
def eating(list: IndexedSeq[Int], i: Int, max: Int, accr: Int = 0): Int = {
list match {
case _ if accr > max => max
case _ if list.isEmpty => accr + i
case x +: xs if i < x => eating(xs, i, max, accr + Math.ceil(x.toDouble / i).toInt - 1)
case x +: xs => eating(xs, i, max, accr)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment