Skip to content

Instantly share code, notes, and snippets.

@waynejo
Last active August 29, 2015 14:14
Show Gist options
  • Save waynejo/3bbdfef9652a24a02f5f to your computer and use it in GitHub Desktop.
Save waynejo/3bbdfef9652a24a02f5f to your computer and use it in GitHub Desktop.
import java.io.{FileOutputStream, FileInputStream}
import scala.io.StdIn
object Main extends App {
Console.setIn(new FileInputStream("B-small-practice.in"))
Console.setOut(new FileOutputStream("B-small-practice.out"))
def solve(input: String): BigInt = {
val in = "0" + input
val (head, tail) = in.splitAt(in.sliding(2, 1).toList.lastIndexWhere((x) => x(0) < x(1)) + 1)
val i = tail.zipWithIndex.filter(_._1 > head.last).min._2
BigInt(head.init + tail(i) + tail.updated(i, head.last).sorted)
}
val cases = StdIn.readLine().toInt
(1 to cases) foreach { n =>
val input = StdIn.readLine()
println(s"Case #$n: ${solve(input)}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment