Skip to content

Instantly share code, notes, and snippets.

@myeesan
Created September 12, 2014 13:33
Show Gist options
  • Save myeesan/4c5ab904b42bdf1761e8 to your computer and use it in GitHub Desktop.
Save myeesan/4c5ab904b42bdf1761e8 to your computer and use it in GitHub Desktop.
import scala.io.Source
import scala.math.min
import java.io.FileOutputStream
import java.io.File
import java.io.PrintWriter
object Osmos {
val in = Source.fromFile("large.in").getLines;
val out = new PrintWriter(new File("large.out"));
case class TestCase(curMoteSize: Int, motes: List[Int], wildCardCount: Int = 0) {
def smallestMote = motes(0)
def minCount(mc:Int) = min(mc, wildCardCount + motes.size)
def solve(preMc:Int = motes.size): Int = {
val mc = minCount(preMc)
if (1 == curMoteSize || motes.isEmpty) {
mc
} else if (curMoteSize > smallestMote) {
new TestCase(curMoteSize + smallestMote, motes.tail, wildCardCount).solve(mc)
} else {
new TestCase(curMoteSize * 2 - 1, motes, wildCardCount + 1).solve(mc)
}
}
}
def main(args: Array[String]) {
val nOfCases = in.next.toInt
val testCases = for {
i <- 1 to nOfCases
val Array(init, _) = in.next.split(" ").map(_.toInt)
val moteList = in.next.split(" ").map(_.toInt).toList
val res = TestCase(init, moteList.sorted).solve()
} {
out.println(s"Case #$i: $res")
println(s"Case #$i: $res")
}
out.flush()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment