-
-
Save krikit/23d0b10c03d1035bbf50 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
object StandingOvation extends App { | |
def toList(str: String): List[Int] = { | |
str.toCharArray.map(_ - '0').toList | |
} | |
def solve(people: String): Int = { | |
@annotation.tailrec | |
def solveInner(people: List[(Int, Int)], standing: Int, invite: Int): Int = { | |
val shy = people.head._1 | |
val level = people.head._2 | |
if (level == 0) people match { | |
case head :: Nil => 0 | |
case _ => solveInner(people.tail, shy, 0) | |
} | |
else people match { | |
case Nil => -1 | |
case head :: Nil => { | |
if (level <= standing) invite | |
else invite + (level - standing) | |
} | |
case head :: tail => { | |
if (level <= standing) solveInner(tail, standing + shy, invite) | |
else { | |
val toInvite = level - standing | |
solveInner(tail, standing + shy + toInvite, invite + toInvite) | |
} | |
} | |
} | |
} | |
solveInner(toList(people).zipWithIndex, 0, 0) | |
} | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = | |
for (i <- 1 to lineIn.next().toInt) { | |
val Array(max, people) = lineIn.next().split(" ") | |
lineOut(s"Case #$i: ${solve(people)}") | |
} | |
val filename = "A-large-practice" | |
val writer = new java.io.PrintWriter(filename + ".out") | |
try { | |
process(io.Source.fromFile(filename + ".in").getLines) { s => | |
writer.println(s); writer.flush() | |
} | |
} finally { | |
writer.flush(); writer.close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment